PHP transaction processing data implementation code. For more information, see.
PHP transaction processing data implementation code. For more information, see.
The Code is as follows:
Public function insertUser ($ userArray ){
Foreach ($ userArray as $ key => $ value ){
@ $ Field. = "$ key ,";
@ $ Content. = "'$ value ',";
}
$ Field = ereg_replace (', $', '', $ field );
$ Content = ereg_replace (', $', '', $ content );
$ Db = db_connect (); // connect to the database
$ Db-> autocommit (FALSE); // set to non-automatic commit-Transaction Processing
$ Sql1 = "insert into t_user (". $ field. ") VALUES (". $ content .")";
$ Result1 = $ db-> query ($ sql1 );
$ Sql2 = "insert into t_userpost (f_username) VALUES ('". $ userArray ['f _ username']. "')";
$ Result2 = $ db-> query ($ sql2 );
If ($ result1 & $ result2 ){
$ Db-> commit (); // all succeeded. The execution result is submitted.
Echo 'Submit ';
} Else {
$ Db-> rollback (); // If any error occurs, roll back and cancel the execution result.
Echo 'rollback ';
}
$ Db-> close ();
}