samedi 2 juillet 2016

Codeigniter transaction not working

I have a function which inserts row into a table, something like below

function fn_insert_user() {
    $this->db->trans_begin();
    $this->db->query('INSERT INTO user VALUES(9,"john", "9865321245")');
    $this->db->query('INSERT INTO user VALUES(8,"martin", "8865321245")');
    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
        echo 'something bad happened';
    }
    else
    {
        $this->db->trans_commit();
        echo 'everything is fine';
    }

}

Now primary key 8 already exists and hence as expected it should not allow to insert second query (which is fine).

It successfully rolls back first query but the problem is that instead of printing 'something bad happened' it prints

A Database Error Occurred
Error Number: 1062
Duplicate entry '8' for key 'PRIMARY'
INSERT INTO user VALUES(8,"martin", "8865321245")
Filename: C:wampwwwlandmarksystemdatabaseDB_driver.php
Line Number: 330

Aucun commentaire:

Enregistrer un commentaire