Undermine2k
Undermine2k

Reputation: 1491

Codeigniter does not execute delete query

I have a problem, when I try to run this function in my model it does nothing. The print statement prints out. 

DELETE FROM child_participantsWHERE Child_Name='test' andParent_username='tester2'

Which when I run from command line works correctly(the record exists and is deleted). But when I try it from my web application it gives me no error but does not actually delete anything. I know i am passing data correctly because I receive it in my controller and model. What gives?

   function remove_child($username, $participant_name) 
    {
    $where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";
    $this->db->where($where, null, false);
    $this->db->delete($this->child_table); 
    echo $this->db->last_query();
    }

Upvotes: 0

Views: 759

Answers (3)

MJ X
MJ X

Reputation: 9044

Check that does your user has delete privilege in the database. if it has than change your code like this:

function remove_child($username, $participant_name) 
{
    $this->db->trans_start();
    $this->db->where('Child_Name',$participant_name);
    $this->db->where('Parent_username',$username);
    $this->db->delete($this->child_table); 
    $this->db->trans_complete();
    return TRUE;
}

i hope that this will solve your problem.

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76666

From the documentation:

If you use multiple function calls they will be chained together with AND between them:

Try changing:

$where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";

to

$this->db->where('Child_Name', $participant_name);
$this->db->where('Parent_username', $username);

// translates to WHERE Child_Name='XXX' and Parent_username='XXX'

Hope this helps!

Upvotes: 4

dasper
dasper

Reputation: 722

Do you get the same results when you break it out into two where method calls? I would do this over how you are using the where method.

$this->db->where('Child_Name',$participant_name);
$this->db->where('Parent_username',$username);
$this->db->delete($this->child_table);

also, turn on the profiler to see all the queries that are being run to make sure there are not other parts of code we cannot see that might be interfering or a transaction not being committed

$this->output->enable_profiler(TRUE);

Another suggestion is the practice of soft deletes so that way your data is not truly gone and also minimizes how much you need to rely on reconstructing your log file. Also to make simple CRUD operations faster you can use a very simple extension of the base model. One that I have used by recommendation is https://github.com/jamierumbelow/codeigniter-base-model

Upvotes: 0

Related Questions