i have displayed table with record and \"Delete\" image. on delete image click i am deleting the record using ajax. supose there are three records with id 40,41,42 if i dele
when we delete from db in codeigniter 2.2.0
using $this->db->delete()
we can operate with two flags:
1. $this->db->_error_message()
and
2. $this->db->affected_rows()
So after db query we get 1 and 2 like:
DELETED: '', 1
NOT DELETED: '', 0 // row with id not found, but sql completed ok
SQL ERROR: string, -1
My choice is the following check:
$this->db->delete($this->table,array('id'=>$id));
if ($this->db->_error_message()) {
$result = 'Error! ['.$this->db->_error_message().']';
} else if (!$this->db->affected_rows()) {
$result = 'Error! ID ['.$id.'] not found';
} else {
$result = 'Success';
}
The db->delete() returns TRUE
, if delete operation is successful. It would only return FALSE
if it COULDN’T delete the row. I think you should be checking something like:
$this->db->affected_rows();
which returns number and not a Boolean, which you can check with your If conditions.