0

我在下面有一个工作的 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();
}
4

1 回答 1

0

这里发生了两个不同的问题。首先,Commands out of sync警告正在发生,因为您在实际完成之前的查询之前尝试启动新查询。有关详细信息,请参阅此答案。基本上,您必须在准备下一个查询之前关闭您正在准备的每个查询。

至于Cannot break/continue错误,这是因为您break 2在只有 1 级深度时调用。break(或)之后的可选数字continue是要突破的“级别”数。

<?php
for($i = 0; $i < 10; $i++){
    // 1 level
    for($j = 0; $j < 10; $j++){
        // 2 levels

        break;      // Break of the $j loop
        break 1;    // Equivalent to above

        break 2;    // Break out of both the $j and $i loops

        break 3;    // Causes an error - there is no third level
    }
}

当然,在那个例子中,它break在击中第一个 s 后永远不会到达另一个 s,但它应该说明这个概念。另请参阅break.

于 2012-10-15T02:12:42.103 回答