Reputation: 404
The actual column names are account and passwd so the second query will failed, but the first query still inserted into database.
$sql1 = "INSERT INTO users (account, passwd) VALUES ('account+1', 'password+1')";
$sql2 = "INSERT INTO users (account, password) VALUES ('account+2', 'password+2')";
try {
$db->beginTransaction();
$db->query($sql1);
$db->query($sql2);
$db->commit();
} catch (Exception $e) {
$db->rollback();
die($e->getMessage());
}
The transaction started successfully with no errors, the problem is that it doesn't rollback, always rows are inserted regardless of failing queries.
MySQL version 5.5.25 and table type is InnoDB.
Upvotes: 0
Views: 511
Reputation: 360872
Did you turn on exceptions? By default pdo doesn't throw them and silently ignores errors.
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Upvotes: 1
Reputation: 3972
$sql1 = "INSERT INTO user (account, passwd) VALUES ('account+1', 'password+1')";
$sql2 = "INSERT INTO users (account, password) VALUES ('account+2', 'password+2')";
try {
$db->beginTransaction();
$db->query($sql1);
$db->query($sql2);
$db->commit();
} catch (Exception $e) {
$db->rollback();
die($e->getMessage());
}
$mysql
refence is wrong, you have to use, $db->rollback();
Upvotes: 0