gpt4 book ai didi

php - 如何使用codeigniter删除数据库中的事件记录

转载 作者:行者123 更新时间:2023-11-29 04:23:05 27 4
gpt4 key购买 nike

Controller :

public function delete()
{
$id=$this->uri->segment(3);
$this->book_model->deletepost($id);
$data['books']=$this->book_model->getposts();
$this->load->view('showbooks',$data);
}

型号:

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');
}

问题是我无法删除记录。这是错误:“where 子句”中的未知列“id”

DELETE FROM `books` WHERE `id` = 0

最佳答案

当您获得帖子时,列名称是 book_id。当你删除的是id。所以也许您需要将其更改为 book_id

此外,在这种情况下,$this->uri->segment(3) 将返回 null,因为 function delete() 没有参数。更多详情请阅读 here

但我会做一些改变:

Controller :

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);
}

型号:

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');
}

关于php - 如何使用codeigniter删除数据库中的事件记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18041681/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com