2016-01-17 46 views
1

我想定製或處理codeigniter中的查詢錯誤刪除,但問題是它不起作用。 如何使用try catch或else語句處理它?如何在刪除錯誤時在codeigniter中捕獲數據庫錯誤?

控制器:

public function delete($id){ 
    $this->load->model('m_customer_info'); 
    $delete=$this->m_customer_info->delete_data($id); 

    $this->get_all_customers(); 
} 

型號:

public function delete_data($id){ 
    $delete="delete from member where idno='$id'"; 

    try{ 
     $this->db->query($delete); 
    } 
    catch(Exception $e) 
    { 
     echo "<script>alert('Cannot delete because it is a club officer')</script>"; 
    } 

    /*if($delete==true){ 
     $this->db->query($delete); 
    } 
    else 
     echo "<script>alert('Cannot delete because it is a club officer')</script>"; 
    */  
} 
+0

檢查[this](http://stackoverflow.com/questions/9093334/codeigniter-db-delete-returns-true-always)q/a。 – Tpojka

回答

1

在模型

在控制器

public function delete($id) 
{ 
    $this->load->model('m_customer_info'); 
    $delete = $this->m_customer_info->delete_data($id); 
    if ($delete == false) { 
     echo "Delete Failed"; 
    } 
    else{ 
     $this->get_all_customers(); 
    } 

} 
+0

仍然顯示如下:[請看看](https://www.dropbox.com/s/bfg900jdq4czo41/catch_error.PNG?dl=0) – HelpMe

+0

Chk錯誤ü刪除有foriegn鍵的行.. @幫助我 – devpro

+1

@HelpMe是的,你不能這樣做。 cz有外鍵。所以創建另一個列調用is_deletd。如果deletd更新爲1,則defult爲0. –