Home >Backend Development >PHP Tutorial >Realize self-worth PHP transaction processing data implementation code
Copy the code 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 successful, submit the execution results
echo 'Submit';
} else {
$db->rollback(); //If any error occurs, roll back and cancel the execution result
echo 'Rollback';
}
$db->close();
}
The above introduces the PHP transaction processing data implementation code for realizing self-worth, including the content of realizing self-worth. I hope it will be helpful to friends who are interested in PHP tutorials.