Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter db->delete() returns true always?

Tags:

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 delete record with ID = 40, responce returns "1" and record gets deleted, next time if i again click on delete image it again returns "1".

codeigniters db->delete() method returns "1" always ? do i need to check manually if record exists and then proceed to delete ? below is my code iin ajax.php

$res = $this->db->delete(tbl_user_groups, array('owner_id' => $admin,'user_group_id'=>$gid));   if($res) echo json_encode (array("success"=>"true")); else     echo json_encode (array("success"=>"false")); 
like image 576
www.amitpatil.me Avatar asked Feb 01 '12 09:02

www.amitpatil.me


People also ask

How to delete data in CodeIgniter 4?

CodeIgniter 4 Model delete() methodTakes a primary key value as the first parameter and deletes the matching record from the model's table: If the model's $useSoftDeletes value is true, this will update the row to set deleted_at to the current date and time.


1 Answers

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.

like image 75
Sudhir Bastakoti Avatar answered Oct 02 '22 15:10

Sudhir Bastakoti