1

控制器:

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

问题是我无法删除记录。这是错误:Unknown column 'id' in 'where clause'

DELETE FROM `books` WHERE `id` = 0
4

2 回答 2

5

当您收到帖子时,列名是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);
}

模型:

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');
}
于 2013-08-04T11:21:37.947 回答
1

打开您的 phpmyadmin 并在此处找到表“books”,单击“查看”,您将获得表(字段)的结构,检查是否有名称为“id”的字段。

第二种方式建议您将短语“'where 子句中的未知列'id'”复制到 translate.google.ru,您会明白它的意思。

所以,否则:如果你有自动增量字段“ID”,它不能为0,也检查一下

于 2013-08-04T10:32:57.517 回答