如何使用代码点火器删除数据库中的活动记录 [英] how to delete active records in the database using code igniter

查看:40
本文介绍了如何使用代码点火器删除数据库中的活动记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器:

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"

The problem is I can't delete the records. This is the error: Unknown column 'id' in 'where clause'

DELETE FROM `books` WHERE `id` = 0

推荐答案

获取帖子时,列名称为 book_id .删除时是 id .因此,也许您需要将其更改为 book_id .

When you get posts the column name is book_id. When you delete is id. So maybe you need to change it to book_id.

在这种情况下, $ this-> uri-&segment(3)也将返回null,因为 function delete()没有参数.有关更多详细信息,请此处

Also $this->uri->segment(3) in this case will return null, because function delete() didn't have parameters. More details read here

但我会做一些更改:

控制器:

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

这篇关于如何使用代码点火器删除数据库中的活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆