0

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();
    }
4
  • $this->db->_error_message(); -- check for errors Commented Jul 8, 2013 at 21:32
  • 1
    Off topic, but you should leverage codeigniter's query composition code to prevent SQL injection attacks. Commented Jul 8, 2013 at 21:32
  • @AmalMurali no error output given. Commented Jul 8, 2013 at 21:33
  • The ` signs may cause problems. Try again after removing them from the $where string. If all fails, use a raw_query. Also have a look at the Codeigniter Database Docs Commented Jul 8, 2013 at 21:37

3 Answers 3

4

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.