控制器:
public function delete()
{
$id=$this->uri->segment(3);
$this->book_model->deletepost($id);
$data['books']=$this->book_model->getposts();
$this->load->view('showbooks',$data);
}
Run Code Online (Sandbox Code Playgroud)
模型:
public function getposts()
{
$posts=$this->db->get('books');
$books=array();
foreach ($posts->result() as $row)
{
$books=array(
'book_id' => $row->bookid,
'book_name' => $row->booktitle,
'book_author' => $row->bookauthor,
'book_year' => $row->bookyear,
'book_isbn' => $row->bookisbn,
'book_publisher' => $row->bookpublisher
);
}
return $books;
}
public function deletepost($id)
{
$this->db->where('id',$id);
$this->db->delete('books');
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法删除记录。这是错误:Unknown column 'id' in 'where clause'
DELETE FROM `books` WHERE `id` = 0
Run Code Online (Sandbox Code Playgroud)
当您收到帖子时,列名称为book_id. 当您删除是id. 所以也许你需要把它改成book_id.
同样$this->uri->segment(3)在这种情况下将返回 null,因为function delete()没有参数。更多详情请阅读此处
但我会做出一些改变:
控制器:
public function delete()
{
$id=$this->uri->segment(3); // Try to write any id here, or in function put parameter
$this->book_model->deletepost($id);
$data['books']=$this->book_model->getposts();
$this->load->view('showbooks',$data);
}
Run Code Online (Sandbox Code Playgroud)
模型:
public function getposts() {
return $this->db->get('books')->result_array();
}
public function deletepost($id) {
$this->db->where('book_id',$id); // I change id with book_id
$this->db->delete('books');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15707 次 |
| 最近记录: |