| <? Php // Database connection $ Conn = mysql_connect ('localhost', 'root ',''); Mysql_select_db ('test', $ conn ); Mysql_query ("set names gbk "); /* The tables that support transactions must be InnoDB A transaction can only appear once: Mysql_query ('start transaction'); // START the TRANSACTION Mysql_query ('rollback'); // roll back the transaction Mysql_query ('commit '); // submit a transaction If multiple rollback transactions occur in a transaction, all database operations are canceled before the first rollback to the start of the transaction when the transaction is committed, after the first rollback, all database operations will still be valid until the transaction is committed. Therefore, the rollback statement is generally placed only before the transaction statement is committed. If a transaction does not have a commit statement, all of the following operations on the database are performed from the start of the transaction (the execution method returns the right or wrong), but the database is not affected, however, when the next transaction statement is executed, the previous transaction is automatically committed. */ Mysql_query ('start transaction '); $ IsBad = 0; $ Ins_testTable1 = "insert into testtable1 (NAME, age) VALUES ('first', 23 )"; If (! Mysql_query ($ ins_testTable1 )){ $ IsBad = 1; } // The insert statement field name is incorrect. $ Ins_testTable2 = "insert into testtable1 (NAME, ages) VALUES ('second', '24 ')"; If (! Mysql_query ($ ins_testTable2 )){ $ IsBad = 1; } If ($ isBad = 1 ){ Echo $ isBad; Mysql_query ('rollback '); } Mysql_query ('commit '); Mysql_close ($ conn ); ?> |