我在mysqli中收到两个错误:
警告:mysqli_stmt :: execute():( HY000 / 2014):命令不同步; 您现在无法在第235-250行的/.../中运行此命令:
命令不同步;你现在无法运行此命令致命错误: 在第241行的/.../中无法中断/继续2个级别
我想知道出现的错误是因为我的查询和插入顺序如下:
<?php
//connect to db
var_dump($_POST);
$optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = ?)";
if (!$optionstmt = $mysqli->prepare($optionquery)) {
// Handle errors with prepare operation here
echo __LINE__.': '.$mysqli->error;
}
$replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = ?)";
if (!$replystmt = $mysqli->prepare($replyquery)) {
// 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();
if ($replystmt->errno)
{
// Handle query error here
echo __LINE__.': '.$replystmt->error;
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();
$results = $_POST['value'];
foreach($results as $id => $value)
{
$answer = $value;
$insert->bind_param("sisiiii", $sessid, $id, $_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;
}
$lastID = $insert->insert_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();
}
?>
答案 0 :(得分:0)
这是因为您正在适时关闭声明
在适当的位置或在另一个查询之前关闭语句$insertanswer->close();