Hi so I know this is something that seems to have been answered before on here, but nothing that I've tried has worked. Basically what I am trying to do is to call up a stored procedure that inserts some data, and gives back the id of the auto_increment that was just inserted. Then that statement is closed and I keep the id.
Then the fun part happens. I go into a loop and for each instance of the loop I need to call another stored procedure that inserts some data, and returns the id of the last auto_increment. Using that id I will then go and do some more things. However, right now it is failing in the loop. It executes the first time without a problem, and then on the prepare for the next go around it gives me the error Commands out of sync; you can't run this command now. 
I really have tried to somehow free up the results from the first time around using stmt->free(), but that didn't work, or to free up the mysqli2 connection, but nothing I've done has worked at this point. Any tips or hints would be really appreciated!
$insert_questionnaire_sql = "CALL insert_questionnaire_info(?, ?, ?, ?, ?, ?, ?)";
$questionnaire_insert_stmt = $mysqli->prepare($insert_questionnaire_sql);
$questionnaire_insert_stmt->bind_param("ssisiis", $meta[0], $meta[4], $length_of_questions, $user, $meta[2], $meta[3], $meta[1]);
//execute the statement
$success = $questionnaire_insert_stmt->execute();
$qn_id = -1;
//bind the id of the questionnaire that was just inserted
$questionnaire_insert_stmt->bind_result($qn_id);
//fetch the id
$questionnaire_insert_stmt->fetch();
//close the statement
$questionnaire_insert_stmt->close();
//next we insert each question into the database
$i = 0;
for($i; $i < count($Questions); $i++){
    //only if the question has been submitted
    if($Questions[$i]->submitted){
        //prepare the statement
        $insert_question_sql = "CALL insert_question_info(?, ?, ?, ?, ?, ?, ?)";
        $question_insert_stmt = $mysqli2->prepare($insert_question_sql) or die ($mysqli2->error);
        $type = -1;
        $width = -1;
        $height = -1;
        //count the number of answers
        $numAnswers = countNotDeletedAnswers($Questions[$i]);
        $text = $Questions[$i]->text;
        //figure out what kind of thing this is
        if($Questions[$i]->instruction == true){
            $type = 2;
        }
        else if($Questions[$i]->image == true){
            $type = 3;
            $width = $Questions[$i]->width;
            $height = $Questions[$i]->height;
            //if we have an image we want to put the path as the text
            $text = $Questions[$i]->path;
        }
        else{
            $type = 1;
        }
        //bind the params
        $question_insert_stmt->bind_param("isiisii", $qn_id, $text, $type, $numAnswers, $user, $width, $height);
        //execute
        $success = $question_insert_stmt->execute() or die ($mysqli2->error);
        //bind the id of the questionnaire that was just inserted
        $q_id = -1;
        $question_insert_stmt->bind_result($q_id);
        //fetch the id
        $data = $question_insert_stmt->fetch();
        //close the statement
        $question_insert_stmt->close();
    }
}
 
    