Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:16:29+00:00 2026-06-13T01:16:29+00:00

I have a working php/mysqli code below where it inserts questions and answers successfully:

  • 0

I have a working php/mysqli code below where it inserts questions and answers successfully:

$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();

}
}

}

}

But a trouble I have been having even before getting the above code to work is that I have 2 additional SELECT queries which I need include in the code above. The queries are known as $replystmt and $optionstmt. The problem though is that if I include those queries in the php/mysqli code above, I keep receiving these errors:

Warning: mysqli_stmt::execute(): (HY000/2014): Commands out of sync;
you can’t run this command now in /insertQuestion.php on line 236 241:

Commands out of sync; you can’t run this command now Fatal error:
Cannot break/continue 2 levels in /insertQuestion.php on line 242

Now the full code is below, my question is that what do I need to change in my code in order for the errors to be removed and the code to work?

Below is the full php/mysqli code:

$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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:16:30+00:00Added an answer on June 13, 2026 at 1:16 am

    There’s two different problems happening here. Firstly, the Commands out of sync warning is happening because you are trying to initiate new queries before actually finishing the previous ones. See this answer for more information about that. Basically, you have to close each of the queries you are preparing before preparing the next one.

    As for the Cannot break/continue error, that is happening because you are calling break 2 when you are only 1 level deep. The optional number after break (or continue) is the number of ‘levels’ to break out of.

    <?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
        }
    }
    

    Of course, in that example it would never reach the other breaks after hitting the first one, but it should illustrate the concept. See also the documentation for break.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning php and OOP programming here. I have below working code I am
I have been working on some code to submit an uploaded image to PHP
I don't know why but the code below is working when I have a
I have a Php page the code for which is below, I am trying
I have the code below and it's working as it should however instead of
I have the following PHP code, which is meant to insert data in the
I have working website in PHP with a MySQL backend which has several tables
My team working on a php/MySQL website for a school project. I have a
I am working on PHP and database MySQL. I have two tables in SQL
I am working on a filter functionality using ajax/jquery and php/mysql.I have two sets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.