Friday, 13 February 2015

PHP & MySQL Transaction mode (Begin , Commit , RollBack)

Table Type : InnoDB
InnoDB Syntax :
  1. CREATE TABLE `customer` (  
  2. .  
  3. .  
  4. .  
  5. .  
  6. ) ENGINE=InnoDB; 

  1. The ‘InnoDB’ feature is disabled; you need MySQL built with ‘InnoDB’ to have it working
    Solution
    my.ini (C:\AppServ\MySQL\my.ini)
    skip-innodb
    Change To
    #skip-innodb
    ### Restart MySQL Service ###
    .
    .
    Example
    php_mysql_transaction1.php
  2. <html>  
  3. <head>  
  4. <title>ShotDev.Com Tutorial</title>  
  5. </head>  
  6. <body>  
  7. <form action="php_mysql_transaction2.php" name="frmAdd" method="post">  
  8. <table width="600" border="1">  
  9. <tr>  
  10. <th width="91"> <div align="center">CustomerID </div></th>  
  11. <th width="160"> <div align="center">Name </div></th>  
  12. <th width="198"> <div align="center">Email </div></th>  
  13. <th width="97"> <div align="center">CountryCode </div></th>  
  14. <th width="70"> <div align="center">Budget </div></th>  
  15. <th width="70"> <div align="center">Used </div></th>  
  16. </tr>  
  17. <tr>  
  18. <td><div align="center"><input type="text" name="txtCustomerID" size="5"></div></td>  
  19. <td><input type="text" name="txtName" size="20"></td>  
  20. <td><input type="text" name="txtEmail" size="20"></td>  
  21. <td><div align="center"><input type="text" name="txtCountryCode" size="2"></div></td>  
  22. <td align="right"><input type="text" name="txtBudget" size="5"></td>  
  23. <td align="right"><input type="text" name="txtUsed" size="5"></td>  
  24. </tr>  
  25. </table>  
  26. <input type="submit" name="submit" value="submit">  
  27. </form>  
  28. </body>  
  29. </html>  
php_mysql_transaction2.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  8. $objDB = mysql_select_db("mydatabase");  
  9.   
  10. //*** Start Transaction ***//  
  11. mysql_query("BEGIN");  
  12.   
  13. //***  Query 1 ***//  
  14. $strSQL = "INSERT INTO customer ";  
  15. $strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";  
  16. $strSQL .="VALUES ";  
  17. $strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";  
  18. $strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";  
  19. $objQuery1 = mysql_query($strSQL);  
  20.   
  21. $strSQL = "INSERT INTO customer ";  
  22. $strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";  
  23. $strSQL .="VALUES ";  
  24. $strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";  
  25. $strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";  
  26. $objQuery2 = mysql_query($strSQL);  
  27.   
  28. if(($objQuery1and ($objQuery2))  
  29. {  
  30. //*** Commit Transaction ***//  
  31. mysql_query("COMMIT");  
  32. echo "Save Done.";  
  33. }  
  34. else  
  35. {  
  36. //*** RollBack Transaction ***//  
  37. mysql_query("ROLLBACK");  
  38. echo "Error Save [".$strSQL."]";  
  39. }  
  40. mysql_close($objConnect);  
  41. ?>  
  42. </body>  
  43. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP MySQL and Transaction (Begin,Commit,RollBack)
.
.

No comments:

Post a Comment