| | |
How to perform a mysql rollback in php?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2004
Posts: 24
Reputation:
Solved Threads: 0
Hi,
I have a situation where i am inserting to two tables from a web form but the first table needs to be inserted to so i can get the PK, then use the PK to insert into the second table. So what I would like to do is rollback the insert operation if there is some error inserting to the first table or vice versa.
This is what i'm thinkin of doin so far:
Any help/ideas/suggestions?
Thanks in advance...
I have a situation where i am inserting to two tables from a web form but the first table needs to be inserted to so i can get the PK, then use the PK to insert into the second table. So what I would like to do is rollback the insert operation if there is some error inserting to the first table or vice versa.
This is what i'm thinkin of doin so far:
php Syntax (Toggle Plain Text)
mysql_query("START TRANSACTION"); if(mysql_query($myquery)) mysql_query("COMMIT"); else mysql_query("ROLLBACK");
Any help/ideas/suggestions?
Thanks in advance...
so pretty much you want to take the insertion uid and use it in the second table.
something like this maybe.
something like this maybe.
PHP Syntax (Toggle Plain Text)
$sql = "SQL HERE"; $query = mysql_query( $sql,$con ); if ( $query ) { $iid = mysql_insert_id( $con ); $sql = "SQL HERE WITH {$iid}"; $query = mysql_query( $sql ); }
This is how I usually do it:
php Syntax (Toggle Plain Text)
<?php $querylog = "TRANSACTION STARTED<br /><br />"; $commit = "commit"; mysql_query("begin", $dbconn); $query = "insert into ..."; if(!mysql_query($query, $dbconn)) { $commit = "rollback"; $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />"; } $query = "insert into ..."; if(!mysql_query($query, $dbconn)) { $commit = "rollback"; $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />"; } $query = "insert into ..."; if(!mysql_query($query, $dbconn)) { $commit = "rollback"; $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />"; } if($commit == "rollback") { $querylog .= "ERROR IN TRANSACTION<br /><br />transaction rolled back<br /><br />"; //echo $querylog; } mysql_query($commit); ?>
Last edited by R0bb0b; Oct 22nd, 2008 at 3:46 am.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
![]() |
Other Threads in the PHP Forum
- Previous Thread: count clicks for flash...
- Next Thread: Putting a for loop in a concatenated string
| Thread Tools | Search this Thread |
advanced apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert ip javascript job joomla js limit link login mail menu mlm mobile multiple mysql oop outofmemmory paging parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty sms soap source space sql stored syntax system table traffic tutorial unicode up-to-date update upload url validator variable video web webapplications xml youtube






