CodeIgniter 的新手,我不知道如何编辑数据库
New to CodeIgniter and I don't know how to edit db
所以我正在尝试学习如何使用 CI 并且希望我学习它的人说它更容易在没有模型的情况下使用它,只有控制器和视图。我正在尝试编辑我的 DB 中的一行。这是我的控制器代码
public function index() {
$alimente = $this->db->query('select alimente.id, alimente.name FROM alimente order by name asc')->result();
$totalQuery = $this->db->query("select count(id) as total from alimente")->result();
$nrcrt = 1;
foreach ($alimente as $a) {
$a->nrcrt = $nrcrt++;
}
$content = $this->parser->parse('alimente/list_alimente', array("ALIMENTE" => $alimente, "COUNT" => $totalQuery), true);
$TITLE = " Lista Alimente";
$array = array('TITLE' => $TITLE, 'CONTENT' => $content);
$this->parser->parse('TEST', $array, false);
}
public function add() {
$add_aliment = $this->parser->parse('alimente/add_aliment', array(), true);
$TITLE = "Adauga alimente";
$array = array('TITLE' => $TITLE, 'CONTENT' => $add_aliment);
$this->parser->parse('TEST', $array, false);
}
public function add_done() {
$name = $this->input->post('name');
$data = array(
'name' => $name,
);
$this->db->insert('alimente', $data);
redirect("alimente");
}
public function edit($id) {
$alimente = $this->db->query('select alimente.id, alimente.name FROM alimente WHERE id = "' . $id . '" order by name desc')->result();
$content = $this->parser->parse('alimente/edit_alimente', array("ALIMENTE" => $alimente), true);
$TITLE = "Modifica";
$array = array('TITLE' => $TITLE, 'CONTENT' => $content);
$this->parser->parse('TEST', $array, false);
}
public function edit_done() {
$name = $this->imput->post("name");
$id = $this->imput->post("id");
$query = $this->db->prepare("update alimente set name = '".$name."' where id = '".$id."'")->result();
$query->execute($name, $id);
redirect("alimente");
}
这是我的观点文件
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Modifica alimente</h1>
</div>
<form method="post" action="{SITE_URL}/alimente/edit_done">
<div class="form-group">
{ALIMENTE}
<label> Id aliment</label>
<input type="text" name="id" value= " {id} " class="form-control" />
<label> Nume aliment</label>
<input type="text" name="name" value= " {name} " class="form-control" />
<br>
{/ALIMENTE}
</div>
<input type="submit" value="Modifica" name="name" class="btn btn-primary" />
</form>
任何帮助都会很棒,如果代码很糟糕,我很抱歉,我刚开始学习编程 3 个月
编辑:我忘记输入我遇到的错误
A PHP Error was encountered Severity: Notice
Message: Undefined property: Alimente::$imput
Filename: controllers/Alimente.php
Line Number: 49
Backtrace:
File: C:\xampp\htdocs\CodeIgnite\application\controllers\Alimente.php
Line: 49 Function: _error_handler
File: C:\xampp\htdocs\CodeIgnite\index.php Line: 315 Function:
require_once
An uncaught Exception was encountered Type: Error
Message: Call to a member function post() on null
Filename:
C:\xampp\htdocs\CodeIgnite\application\controllers\Alimente.php
Line Number: 49
Backtrace:
File: C:\xampp\htdocs\CodeIgnite\index.php Line: 315 Function:
require_once
首先 CI 是一个 MVC 框架,所以所有与数据库相关的东西都应该交给模型。
如果您尝试从控制器或视图访问数据库。这是对架构的破坏,从控制器访问数据库不是一个好习惯。尝试创建单独的文件,例如
employee.php (html) 所有 html 都将放在 applciaiton/view 文件夹中
Employee.php(控制器)所有 url 方法都将命中,并从那里调用模型中的适当方法。将进入 applciaiton/controller 文件夹
Employee_model(模型)其中所有向数据库中插入更新和删除的函数都可以在模型中使用。
我会推荐你看一些教程来理解 MVC 的概念
Change imput into input then try
public function edit_done() {
$name = $this->input->post("name");
$id = $this->input->post("id");
$query = $this->db->prepare("update alimente set name = '".$name."' where id = '".$id."'")->result();
$query->execute($name, $id);
redirect("alimente");
}
所以我正在尝试学习如何使用 CI 并且希望我学习它的人说它更容易在没有模型的情况下使用它,只有控制器和视图。我正在尝试编辑我的 DB 中的一行。这是我的控制器代码
public function index() {
$alimente = $this->db->query('select alimente.id, alimente.name FROM alimente order by name asc')->result();
$totalQuery = $this->db->query("select count(id) as total from alimente")->result();
$nrcrt = 1;
foreach ($alimente as $a) {
$a->nrcrt = $nrcrt++;
}
$content = $this->parser->parse('alimente/list_alimente', array("ALIMENTE" => $alimente, "COUNT" => $totalQuery), true);
$TITLE = " Lista Alimente";
$array = array('TITLE' => $TITLE, 'CONTENT' => $content);
$this->parser->parse('TEST', $array, false);
}
public function add() {
$add_aliment = $this->parser->parse('alimente/add_aliment', array(), true);
$TITLE = "Adauga alimente";
$array = array('TITLE' => $TITLE, 'CONTENT' => $add_aliment);
$this->parser->parse('TEST', $array, false);
}
public function add_done() {
$name = $this->input->post('name');
$data = array(
'name' => $name,
);
$this->db->insert('alimente', $data);
redirect("alimente");
}
public function edit($id) {
$alimente = $this->db->query('select alimente.id, alimente.name FROM alimente WHERE id = "' . $id . '" order by name desc')->result();
$content = $this->parser->parse('alimente/edit_alimente', array("ALIMENTE" => $alimente), true);
$TITLE = "Modifica";
$array = array('TITLE' => $TITLE, 'CONTENT' => $content);
$this->parser->parse('TEST', $array, false);
}
public function edit_done() {
$name = $this->imput->post("name");
$id = $this->imput->post("id");
$query = $this->db->prepare("update alimente set name = '".$name."' where id = '".$id."'")->result();
$query->execute($name, $id);
redirect("alimente");
}
这是我的观点文件
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Modifica alimente</h1>
</div>
<form method="post" action="{SITE_URL}/alimente/edit_done">
<div class="form-group">
{ALIMENTE}
<label> Id aliment</label>
<input type="text" name="id" value= " {id} " class="form-control" />
<label> Nume aliment</label>
<input type="text" name="name" value= " {name} " class="form-control" />
<br>
{/ALIMENTE}
</div>
<input type="submit" value="Modifica" name="name" class="btn btn-primary" />
</form>
任何帮助都会很棒,如果代码很糟糕,我很抱歉,我刚开始学习编程 3 个月
编辑:我忘记输入我遇到的错误
A PHP Error was encountered Severity: Notice
Message: Undefined property: Alimente::$imput
Filename: controllers/Alimente.php
Line Number: 49
Backtrace:
File: C:\xampp\htdocs\CodeIgnite\application\controllers\Alimente.php Line: 49 Function: _error_handler
File: C:\xampp\htdocs\CodeIgnite\index.php Line: 315 Function: require_once
An uncaught Exception was encountered Type: Error
Message: Call to a member function post() on null
Filename: C:\xampp\htdocs\CodeIgnite\application\controllers\Alimente.php
Line Number: 49
Backtrace:
File: C:\xampp\htdocs\CodeIgnite\index.php Line: 315 Function: require_once
首先 CI 是一个 MVC 框架,所以所有与数据库相关的东西都应该交给模型。 如果您尝试从控制器或视图访问数据库。这是对架构的破坏,从控制器访问数据库不是一个好习惯。尝试创建单独的文件,例如
employee.php (html) 所有 html 都将放在 applciaiton/view 文件夹中 Employee.php(控制器)所有 url 方法都将命中,并从那里调用模型中的适当方法。将进入 applciaiton/controller 文件夹
Employee_model(模型)其中所有向数据库中插入更新和删除的函数都可以在模型中使用。
我会推荐你看一些教程来理解 MVC 的概念
Change imput into input then try
public function edit_done() {
$name = $this->input->post("name");
$id = $this->input->post("id");
$query = $this->db->prepare("update alimente set name = '".$name."' where id = '".$id."'")->result();
$query->execute($name, $id);
redirect("alimente");
}