| | |
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
Views: 4367 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery js limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube






