I have this function in my model that is supposed to run two queries in a transaction, but the update query is not working.
public function delete($id = null)
{
if($id) {
$delete = "DELETE FROM borrowed_books
WHERE id = '$id'; ";
$mod="UPDATE `books` b
INNER JOIN `borrowed_books` a
SET b.nr_copies=b.nr_copies+1
WHERE b.id_book=a.id_book AND a.id = '$id'; ";
$this->db->trans_start();
$this->db->query($delete);
$this->db->query($mod);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
return false;
} else {
return true;
}
}
}
A quickfix might be executing the update before deleting;
public function delete($id = null)
{
if ($id) {
$delete = "DELETE FROM borrowed_books
WHERE id = '$id'; ";
$mod="UPDATE `books` b
INNER JOIN `borrowed_books` a
SET b.nr_copies=b.nr_copies+1
WHERE b.id_book=a.id_book AND a.id = '$id'; ";
$this->db->trans_start();
// execute the update
$this->db->query($mod);
// then delete
$this->db->query($delete);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
return false;
} else {
return true;
}
}
}
The update query condition; it is looking for data which is not existent because you deleted it. So if you do the update (then your query finds result assuming that the condition is correct), it will update..