Home  >  Article  >  Backend Development  >  PHP transaction processing data implementation code_PHP tutorial

PHP transaction processing data implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:171168browse

Copy 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 result
echo 'Submit';
} else {
$db->rollback(); //If any error occurs, roll back and cancel the execution result
echo 'Rollback';
}
$db->close();
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321765.htmlTechArticleCopy the code The code is as follows: public function insertUser ($userArray){ foreach ($userArray as $key = $value) { @$field .= "$key,"; @$content .= "'$value',"; } $field = ereg_replace(',...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn