我有以下代码:
// open the database connection
require_once('php/db/connect.php');
// set a log file
$logFile = 'log/php.log';
// set the error and the doCommit to false
$error = false;
$doCommit = false;
// build the query
$query = "SELECT name,gender,email,country FROM tmp_backer WHERE backerid = ?";
// prepare the query
if ($stmt = $mysqli->prepare($query)) {
// bind the parameters
$id = $postArray['backerid'];
$stmt->bind_param('s', $id);
// execute the query
if ($stmt->execute()) {
// the query is executed!
$stmt->store_result();
$tmpArray = array();
$stmt->store_result();
$stmt->bind_result( $name,$gender,$email,$country);
$stmt->fetch();
// clear the parameters
$query = '';
// copy everything to the table 'backer'
// build the query
$query = "INSERT INTO backer (backerid,name,gender,email,country) VALUES (?,?,?,?,?)";
// prepare the query
if ($stmt = $mysqli->prepare($query)) {
// bind the parameters
$stmt->bind_param(
'ssiss',
$id,
$name,
$gender,
$email,
$country
);
if ($stmt->execute()) {
// the query is executed!
// set doCommit to true
$doCommit = true;
} else {
// the query could not be executed
// log this into the log file and send the visitor back to the become a backer page
error_log(date('Y-m-d H:i:s').' | query could not be executed (INSERT query) '.$stmt->error.PHP_EOL,3,$logFile);
$error = true;
};
} else {
// the query is invalid
// log this into the log file and send the visitor back to the become a backer page
error_log(date('Y-m-d H:i:s').' | query is invalid (INSERT query) '.$stmt->error.PHP_EOL,3,$logFile);
$error = true;
};
} else {
// the query could not be executed
// log this into the log file and send the visitor back to the become a backer page
error_log(date('Y-m-d H:i:s').' | query could not be executed (SELECT query) '.$stmt->error.PHP_EOL,3,$logFile);
$error = true;
};
} else {
// the query is invalid
// log this into the log file and send the visitor back to the become a backer page
error_log(date('Y-m-d H:i:s').' | query is invalid (SELECT query) '.$stmt->error.PHP_EOL,3,$logFile);
$error = true;
};
// check if there where any errors whilst preparing/executing
// the query
if ($error == true) {
// there were errors
$mysqli->rollback();
exit();
} else {
// there were no errors
// doCommit was set to true
if ($doCommit == true) {
$mysqli->commit();
};
};
// close the db connection
require_once('php/db/close.php');
第二次查询执行两次,因为我在包含以下文本的日志文件中收到错误:
2015-09-14 21:14:12 |查询无法执行(INSERT查询)重复条目' qzdkzkdjzdk'关键' PRIMARY'
backerID
是生成的随机密钥,是两个表中的主键(字段:backerid
)。查询WORKS,所以数据从表tmp_backer复制到支持者,但问题是页面没有显示(因为第二次执行INSERT查询时,它会遇到错误,所以它会转到它所在的部分喜欢:$mysqli->rollback(); exit();
任何人都可以帮我吗?
答案 0 :(得分:-1)
删除此行:
if ($doCommit == true) {
$mysqli->commit();
};
并将其替换为:
$mysqli->commit();
因为if($error == true)
已被调用,所以在" else"之后不会有任何其他事情发生。但如果它是"假"因为它是"假" $doCommit
必须是" true",所以,自动"提交它"而不是回电话。问题是,你让$doCommit
激活了两次。首先,通过常规电话再次当你要求"它"如果结果是真的。