0

我在 mysqli 中收到两个错误:

警告:mysqli_stmt::execute(): (HY000/2014): 命令不同步;您现在无法在 /.../ 第 235 240 行运行此命令:

命令不同步;您现在无法运行此命令 致命错误:无法在第 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();
}

?>
4

1 回答 1

0

这是因为您在适当的时间关闭语句

$insertanswer->close();在适当的位置或在另一个查询之前关闭语句

于 2012-10-14T16:12:42.377 回答