0

我无法插入QuestionId表格Answer

Question插入没有问题,但我试图QuestionId从表中检索,Question这是一个自动增量到Answer表中,通过查找SessionIdQuestionNo那个特定的QuestionId属于。相反,它在插入表格时一直显示0QuestionIdAnswer

现在我知道尝试QuestionIdQuestion表中执行选择的查询是正确的,因为这已经过测试。我认为问题在于我将代码放在哪里,但我不确定?

我想使用 SELECT 方法来尝试查找问题编号,我不想使用 mysqli->insert_id。

这是数据库表:

问题表

QuestionId (auto)  SessionId  QuestionNo
4                  2          1
5                  2          2
6                  2          3

目前答题表:

AnswerId (auto)  QuestionId  Answer
7                0           A
8                0           C
9                0           A
10               0           B
11               0           True

答案表应如下所示:

AnswerId (auto)  QuestionId  Answer
7                4           A
8                4           C
9                5           A
10               5           B
11               6           True

下面是代码:

 $questionsql = "INSERT INTO Question (SessionId, QuestionNo) 
    VALUES (?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
    // Handle errors with prepare operation here
    echo __LINE__.': '.$mysqli->error;
}

 $answersql = "INSERT INTO Answer (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++ )
    {


            $insert->bind_param("ii", $sessionid, $_POST['numQuestion'][$i]);

            $insert->execute();

            if ($insert->errno) 
            {
                // Handle query error here
                echo __LINE__.': '.$insert->error;
                break 1;
            }
}

        $results = $_POST['value'];
        foreach($results as $id => $value) 
        {
            $answer = $value;

            $lastID = $id;

            $questionidquery = "SELECT QuestionId FROM Question WHERE (QuestionNo = ? AND SessionId = ?)";

        if (!$questionidstmt = $mysqli->prepare($questionidquery)) {
        // Handle errors with prepare operation here
          echo __LINE__.': '.$mysqli->error;
        }


        // Bind parameter for statement
        $questionidstmt->bind_param("ii", $lastID, $sessionId);

        // Execute the statement
        $questionidstmt->execute();

                    if ($questionidstmt->errno) 
                    {
                        // Handle query error here
                        echo __LINE__.': '.$questionidstmt->error;
                        break 2;
                    }

        // This is what matters. With MySQLi you have to bind result fields to
        // variables before calling fetch()
        $questionidstmt->bind_result($quesid);

        // This populates $optionid
        $questionidstmt->fetch(); 

        $questionidstmt->close(); 



            foreach($value as $answer) 
            {
                $insertanswer->bind_param("is", $quesid, $answer);

                $insertanswer->execute();

                if ($insertanswer->errno) {
                    // Handle query error here
                    echo __LINE__.': '.$insertanswer->error;
                    break 3;
                }
            }
        }


    //close your statements at the end


    $insertanswer->close();
    $insert->close();
}

?>

下面是 var_dump($_POST):

array(8) { 
["numberAnswer"]=> array(2) { 
         [0]=> string(1) "1" 
         [1]=> string(1) "1" 
         } 
         ["numQuestion"]=> array(2) 
         { 
         [0]=> string(1) "1" 
         [1]=> string(1) "2" 
         } 
         ["questionText"]=> array(2) { 
         [0]=> string(12) "What is 2+2?" 
         [1]=> string(12) "What is 4+4?" 
         } 
         ["gridValues"]=> array(2) { 
         [0]=> string(1) "4" 
         [1]=> string(1) "4" 
         } 
         ["reply"]=> array(2) { 
         [0]=> string(6) "single" 
         [1]=> string(6) "single" 
         } 
         ["textWeight"]=> array(2) { 
         [0]=> string(1) "5" 
         [1]=> string(1) "5" 
         } 
         ["submitDetails"]=> string(14) "Submit Details" ["value"]=> array(2) { 
         [1]=> array(1) { 
         [0]=> string(1) "B" 
         } 
         [2]=> array(1) { 
         [0]=> string(1) "D" 
         } 
         } 
         } 
4

1 回答 1

0

您可以通过以下方式找出最后插入的 id:

SELECT LAST_INSERT_ID();

请注意,该值只会返回最后一次插入,因此如果您想做更多插入,则必须保存它以供重复使用:

伪代码:

  • 插入问题
  • 运行查询SELECT LAST_INSERT_ID()并将应用层中的值保存到变量qid
  • 插入答案,qid用于答案行的 question_id
于 2013-01-07T23:29:57.170 回答