Search code examples
drupal-7

How can I use db_delete with multiple conditions?


I am trying to do a query like "DELETE FROM ac_journal_term WHERE jid = $jid AND tid = $tid";

foreach ($form_state["values"]["existing_tags"] as $tid)
{
  db_delete('ac_journal_term') 
  ->condition('jid', $jid)->condition('tid', $tid)
  ->execute();
}

I do not think this is the correct way to do it, but I could not find an example online on how to do multiple conditions with the db_delete.


Solution

  • db_delete follows the usual condition conventions.

    foreach ($form_state["values"]["existing_tags"] as $tid)
    {
      $and = db_and()->condition('jid', $jid)->condition('tid', $tid);
      db_delete('ac_journal_term') 
      ->condition($and)
      ->execute();
    }
    

    See condition clause documentation here:

    https://drupal.org/node/310086