943,657 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 18128
  • PHP RSS
Oct 21st, 2008
0

How to perform a mysql rollback in php?

Expand Post »
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:
php Syntax (Toggle Plain Text)
  1. mysql_query("START TRANSACTION");
  2. if(mysql_query($myquery))
  3. mysql_query("COMMIT");
  4. else
  5. mysql_query("ROLLBACK");

Any help/ideas/suggestions?

Thanks in advance...
Reputation Points: 10
Solved Threads: 0
Light Poster
ray_broome is offline Offline
25 posts
since Aug 2004
Oct 21st, 2008
0

Re: How to perform a mysql rollback in php?

so pretty much you want to take the insertion uid and use it in the second table.

something like this maybe.

PHP Syntax (Toggle Plain Text)
  1. $sql = "SQL HERE";
  2. $query = mysql_query( $sql,$con );
  3. if ( $query ) {
  4. $iid = mysql_insert_id( $con );
  5. $sql = "SQL HERE WITH {$iid}";
  6. $query = mysql_query( $sql );
  7. }
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 22nd, 2008
0

Re: How to perform a mysql rollback in php?

This is how I usually do it:
php Syntax (Toggle Plain Text)
  1. <?php
  2. $querylog = "TRANSACTION STARTED<br /><br />";
  3. $commit = "commit";
  4. mysql_query("begin", $dbconn);
  5.  
  6. $query = "insert into ...";
  7. if(!mysql_query($query, $dbconn))
  8. {
  9. $commit = "rollback";
  10. $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />";
  11. }
  12.  
  13. $query = "insert into ...";
  14. if(!mysql_query($query, $dbconn))
  15. {
  16. $commit = "rollback";
  17. $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />";
  18. }
  19.  
  20. $query = "insert into ...";
  21. if(!mysql_query($query, $dbconn))
  22. {
  23. $commit = "rollback";
  24. $querylog .= "error in query: " . $query . " : " . mysql_error($dbconn) . "<br /><br />";
  25. }
  26.  
  27. if($commit == "rollback")
  28. {
  29. $querylog .= "ERROR IN TRANSACTION<br /><br />transaction rolled back<br /><br />";
  30. //echo $querylog;
  31. }
  32.  
  33. mysql_query($commit);
  34. ?>
Last edited by R0bb0b; Oct 22nd, 2008 at 3:46 am.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 22nd, 2008
0

Re: How to perform a mysql rollback in php?

cool, thanks for the help guys, r0bb0b's solution seems closer to what i'm trying to do though because it is supposed to be an "all or nothing" situation
Reputation Points: 10
Solved Threads: 0
Light Poster
ray_broome is offline Offline
25 posts
since Aug 2004
Apr 10th, 2011
0
Re: How to perform a mysql rollback in php?
Make sure that your Storage Engine is set to InnoDB.
Attached Thumbnails
Click image for larger version

Name:	InnoDB.jpg
Views:	190
Size:	38.5 KB
ID:	20542  
Reputation Points: 10
Solved Threads: 1
Newbie Poster
onlysumon is offline Offline
1 posts
since Apr 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Preg_Replace and match YouTube link
Next Thread in PHP Forum Timeline: How to show the time and data in the user's timezone?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC