1

I have a function which allow me to delete a row on a table. But the problem is that, for database integrity, I have to delete another row on another table that is in relation with the record I want to delete. This is my function :

public function delete($tab)
    {
        $this->db->where('id', $tab['id']);
        $this->db->delete('agent', $tab); 
       // Here the query is "DELETE from agent WHERE id = $tab['id']"

        $this->db->where('id_agent', $tab['id']);
        $this->db->delete('agent_vendeuse', $tab);
// Here the query gives "Delete from agent_vendeuse WHERE id_agent= $tab['id'] AND id=$tab['id'];" Which is where the error comes from
    }

I have an error database error on my function :

DELETE FROM `agent_vendeuse` WHERE `id_agent` = '2' AND `id` = '2'

Which means that after I affected the new index 'id_agent' for the where clause, the previous one 'id' is still in cache.

The $tab variable is coming from $_POST of my form. I just changed its name in function.

I guess I have to clean the cache after the first deletion but how to write it ?

4
  • 1
    Can you post the actual error message you get? Commented Mar 13, 2017 at 10:22
  • avenir.ro/… -- a little off the track, but this might help in making relations in CI Commented Mar 13, 2017 at 10:25
  • @Pacio I got a codeigniter error on the second query because I was having 2 conditions on my where clause id and id_agent which are on different tables Commented Mar 13, 2017 at 10:32
  • Ok, understand now. But what is that $tab variable you're passing to delete()? The second parameter should be a where clause, but you've already done that with the where() call. Commented Mar 13, 2017 at 10:45

1 Answer 1

1

The second parameter of the delete function is a where clause, so I'm guessing that probably what happens is that the line $this->db->where('id', $tab['id']); gets ignored on the first delete() call, and for some reason on the second delete() call the 'where clause' you're defining in the parameter as $tab gets ignored and the two where() calls gets used instead.

Just remove the $tab from the parameters passed to delete() and you should be ok:

public function delete($tab)
{
    $this->db->where('id', $tab['id']);
    $this->db->delete('agent'); 

    $this->db->where('id_agent', $tab['id']);
    $this->db->delete('agent_vendeuse');
}
Sign up to request clarification or add additional context in comments.

3 Comments

agent and agent_vendeuse are two different table and agent_vendeuse does not have id on its fields. This is why the second deletion throws a database error. Codeigniter kept the two where on its query
I understand that they are different tables and that it kept the two where(), but I'm pretty sure that's not normal behaviour and that it happened because the first delete() had inappropriate parameters. Did you try this code? You could also try to replace all four lines with only $this->db->delete('agent', array('id'=>$tab['id']); and $this->db->delete('agent_vendeuse', array('id'=>$tab['id']);
Yes and it works! Thanks. I was not using well the delete function of codeigniter!

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.