3

问题:

想要在下面执行一个 Select Query(必须是这个查询):

从 Question WHERE 中选择 QuestionId (QuestionNo = ? AND SessionId = ?)

为了能够在 Question 表中找到 QuestionId 并将其存储在 Answer Table 中的所有答案,以便我们可以确定哪些答案属于哪个问题

问题:

mysqli 代码的问题是它无法插入正确的 QuestionId 值。它在表中一直为 QuestionId 显示 0 Answer。那么有人可以解决这个问题以便能够显示正确的 QuestionId 吗?

它必须完成顶部提供的 SELECT 查询。我必须在mysqli中使用它。

这是数据库表:

问题表

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

?>
4

1 回答 1

1

您需要在 INSERT 之后直接检索用作问题中自动递增 id 的序列的最后一个值 - 使用 LAST_INSERT_ID() SQL 函数执行此操作。然后,您可以在插入 Answer 时将此值用作参数。

这是一篇关于如何做到这一点的文章。

您还可以通过在插入答案循环中消除对问题 ID 的查询来重构您的代码。相反,当您插入问题时,使用每个 QuestionNo 的 QuestionId 填充关联数组。在遍历答案时,使用关联数组快速检索 QuestionId。记忆不应该是一场音乐会,因为您目前在 HTTP 请求中拥有所有问题和答案。

代码的核心将如下所示:

// AA: Declare an empty associative array for QuestionNo -> QuestionID
$question_ids = array()

for($i = 0;  $i < $c; $i++ )
{
        // AA: Extract the QuestionNo for multiple use
        $questionNo = $_POST['numQuestion'][$i];

        $insert->bind_param("ii", $sessionid, $questionNo);

        $insert->execute();

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

        // AA: Retrieve the questionId from MySQL
        $questionId = mysql_insert_id();

        // AA: Add a key-value pair (QuestionNo, QuestionId) to $question_ids
        $question_ids[$questionNo] = $questionId;
}

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

    // AA: Look up the QuestionId for the question number
    $quesid = $question_ids[$id];

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

注意:我不是 PHP 程序员,也没有对此进行测试,因此可能存在语法错误。对不起 :(

于 2013-01-08T07:32:49.630 回答