我无法将QuestionId插入Answer表中。

Question插入没有问题,但是我试图从QuestionId表中检索Question,这是通过查找特定属于。相反,当插入Answer表时,它会一直显示SessionIdQuestionNo

现在,我知道尝试对QuestionId表中的每个0执行选择的查询是正确的,因为已经对此进行了测试。我认为问题出在我放置代码的位置,但是我不确定吗?

这是数据库表:

问题表

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

最佳答案

现在,您的QuestionID来自$_POST['value'],这可能不是您想要的...在插入每个问题之后,您需要从mysql获取自动生成的QuestionID。

您可以使用$mysqli->insert_id检索最后生成的ID。在每次调用$insert->execute();之后执行此操作,然后在插入答案时使用这些ID。

编辑1:

您应该使用$mysqli->insert_id,因为insert_id仅对连接对象有效(您称为$mysqli)。

您还需要将insert_id分配给某些内容。我在遵循您的代码时遇到麻烦,但是我想您想要的是将问题ID存储在数组中以备后用,如下所示:

$question_ids[$i] = $mysqli->insert_id


我对您在此之后的工作感到茫然,我想您输入了带有相应问题编号的答案,但是...也许如果您可以发布var_dump($_POST)的输出,我们将会更好地理解。无论如何,您都将需要某种循环来插入每个问题的答案,并且您可以(希望)使用数组$question_ids[$i]来检索QuestionID。

编辑2:

这是一些示例代码(未经测试),请尝试在使用$_POST$_SESSION之前使其正常工作

<?php
$questions = array(
    array(2, 1),
    array(2, 2),
    # etc
);
$answers = array(
    array(A, C),
    array(A, B),
);

$questionsql = "INSERT INTO Question (SessionId, QuestionNo) VALUES (?, ?)";
if (!$insert = $mysqli->prepare($questionsql)) {
    die('couldn\'t prepare statement 1');
}

$answersql = "INSERT INTO Answer (QuestionId, Answer) VALUES (?, ?)";
if (!$insertanswer = $mysqli->prepare($answersql)) {
    die('couldn\'t prepare statement 2');
}

$c = count($questions);

for($i = 0;  $i < $c; $i++)
{
    $insert->bind_param("ii", $questions[$i][0], $questions[$i][1]);
    $insert->execute();

    if ($insert->errno)
    {
        die("Error inserting question $i");
    }

    $lastID = $mysqli->insert_id;

    foreach ($ans in $answers[$i])
    {
        $insertanswer->bind_param("is", $lastID, $ans);

        $insertanswer->execute();

        if ($insertanswer->errno) {
            die("Error inserting answer to question $i");
        }
    }

}

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

关于php - 它不在答案数据库表中插入questionid,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14194664/

10-10 11:47