fomalhaut
Member
- Messages
- 107
- Reaction score
- 0
- Points
- 16
Hello.
In Php/Mysql, I want to insert rows into two inter-dependant tables T1 and T2. That is, when I insert 1 row into T1, I MUST insert the corresponding into T2.
I've written two prepare statements for these inserts : $ins1 and $ins2, refering the two tables.
Am I right if I do that ?
Thanks for your answers.
In Php/Mysql, I want to insert rows into two inter-dependant tables T1 and T2. That is, when I insert 1 row into T1, I MUST insert the corresponding into T2.
I've written two prepare statements for these inserts : $ins1 and $ins2, refering the two tables.
PHP:
$dbh->beginTransaction();
$res1 = $ins1->execute(); // this (try to) execute the insert in the first table
if (!$res1) {
$dbh->rollback(); // an error was found: don't insert in the second table
}
else {
$res2 = $ins2->execute(); // this execute the insert in the second table
if (!$res2) {
$dbh->rollback(); // an error was found: don't validate any inserts
}
else {
$dbh->commit(); // tables have twice been updated (by inserts)
}
}
Thanks for your answers.