0

mysqliで2つのエラーが発生します。

警告:mysqli_stmt :: execute():(HY000 / 2014):コマンドが同期していません。235240行目の/.../でこのコマンドを実行することはできません。

コマンドが同期していません。現在、このコマンドを実行できません。致命的なエラー: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 に答える