2013-08-04 47 views
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 
+0

您已在表中使用的是它是'book_id'或'id'?,在你使用book_id not id的選擇查詢中,所以一旦檢查你的db –

回答

1

打開你的phpmyadmin,並在這裏找到表「書」,點擊「查看」,你會得到表(場)的結構,檢查是否有字段名「 ID」。

第二種方式建議您在'where子句'中將短語「未知列'id'複製到translate.google.ru,您會明白它的意思。

所以,其他:如果您有場「ID」與自動增量,它不能爲0,檢查它太

3

當你的職位列名是book_id。當您刪除時是id。所以也許你需要把它改成book_id

另外$this->uri->segment(3)在這種情況下會返回null,因爲function delete()沒有參數。更多細節讀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'); 
}