我在下面有一个工作的 php/mysqli 代码,它成功地插入了问题和答案:
$i = 0;
$c = count($_POST['numQuestion']);
$questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent)
VALUES (?, ?, ?)";
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
} else{
for($i = 0; $i < $c; $i++ ){
$results = $_POST['value'];
foreach($results as $id => $value) {
$answer = $value;
$insert->bind_param("sis", $sessid, $id, $_POST['questionText'][$i]);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$lastID = $insert->insert_id;
$insert->close();
foreach($value as $answer) {
$answersql = "INSERT INTO Answer (SessionId, QuestionId, Answer)
VALUES (?, ?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
}
$insertanswer->bind_param("sis", $sessid, $lastID, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
}
$insertanswer->close();
}
}
}
}
但是,在上述代码运行之前,我一直遇到的一个问题是,我需要在上面的代码中包含 2 个额外的 SELECT 查询。这些查询称为$replystmt
和$optionstmt
。但问题是,如果我在上面的 php/mysqli 代码中包含这些查询,我会不断收到这些错误:
警告:mysqli_stmt::execute(): (HY000/2014): 命令不同步;您现在无法在第 236 241 行的 /insertQuestion.php 中运行此命令:
命令不同步;您现在无法运行此命令 致命错误:无法在第 242 行的 /insertQuestion.php 中中断/继续 2 个级别
现在完整的代码如下,我的问题是我需要在我的代码中进行哪些更改才能删除错误并使代码正常工作?
下面是完整的 php/mysqli 代码:
$replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = ?)";
if (!$replystmt = $mysqli->prepare($replyquery)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
$optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = ?)";
if (!$optionstmt = $mysqli->prepare($optionquery)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
// Prepare your statements ahead of time
$questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, NoofAnswers, ReplyId, QuestionMarks, OptionId)
VALUES (?, ?, ?, ?, ?, ?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
$answersql = "INSERT INTO Answer (SessionId, QuestionId, Answer)
VALUES (?, ?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
//make sure both prepared statements succeeded before proceeding
if( $insert && $insertanswer)
{
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
$c = count($_POST['numQuestion']);
for($i = 0; $i < $c; $i++ )
{
$selected_option = "A-C";
$selected_reply = "Single";
// Bind parameter for statement
$optionstmt->bind_param("s", $selected_option);
// Execute the statement
$optionstmt->execute();
if ($optionstmt->errno)
{
// Handle query error here
echo __LINE__.': '.$optionstmt->error;
break 1;
}
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$optionstmt->bind_result($optionid);
// This populates $optionid
$optionstmt->fetch();
// Bind parameter for statement
$replystmt->bind_param("s", $selected_reply);
// Execute the statement
$replystmt->execute(); //Line 236
if ($replystmt->errno)
{
// Handle query error here
echo __LINE__.': '.$replystmt->error; //Line 241
break 2;
}
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$replystmt->bind_result($replyid);
// This populates $optionid
$replystmt->fetch();
$insert->bind_param("sisiiii", $sessid, $_POST['numQuestion'][$i], $_POST['questionText'][$i],
$_POST['numberAnswer'][$i], $replyid, $_POST['textWeight'][$i],
$optionid);
$insert->execute();
if ($insert->errno)
{
// Handle query error here
echo __LINE__.': '.$insert->error;
break 3;
}
}
$results = $_POST['value'];
foreach($results as $id => $value)
{
$answer = $value;
$lastID = $id;
foreach($value as $answer)
{
$insertanswer->bind_param("sis", $sessid, $lastID, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__.': '.$insertanswer->error;
break 4;
}
}
}
//close your statements at the end
$insertanswer->close();
$insert->close();
$replystmt->close();
$optionstmt->close();
}