SQL插入查询不是';t从PHP web应用程序更新列

SQL插入查询不是';t从PHP web应用程序更新列,php,sql,database,database-design,Php,Sql,Database,Database Design,我最近修改了一些代码,使quick.php脚本能够容纳多个测验,而不是一个测验。为此,当用户单击测验链接时,我发送了quick\u id和quick\u title变量,并使用$\u GET接收它们。但是,一旦提交了测验表格,测验id列不再在高分表中更新 下面是quick.php的代码 <?php // Start the session require_once('startsession.php'); // Insert the Page Header

我最近修改了一些代码,使quick.php脚本能够容纳多个测验,而不是一个测验。为此,当用户单击测验链接时,我发送了
quick\u id
quick\u title
变量,并使用$\u GET接收它们。但是,一旦提交了测验表格,测验id列不再在高分表中更新

下面是quick.php的代码

<?php
    // Start the session
    require_once('startsession.php');

    // Insert the Page Header
    $page_title = "Quiz Time!";
    require_once('header.php');

    require_once('connectvars.php');

    // Make sure user is logged in
    if (!isset($_SESSION['user_id'])) {
        echo '<p>Please <a href="login.php">log in</a> to access this page.</p>';
        exit();
    }

    // Show navigation menu
    require_once('navmenu.php');

    // Connect to database
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    // Declare $quiz_id
    $quiz_title = $_GET['title'];
    $quiz_id = $_GET['id'];
    // print_r($quiz_title);
    // print_r($quiz_id);

    // Grab list of question_id's for this quiz
    $query = "SELECT question_id FROM question WHERE quiz_id = '" . $quiz_id . "'";
    $data = mysqli_query($dbc, $query);
    $questionIDs = array();
    while ($row = mysqli_fetch_array($data)) {
        array_push($questionIDs, $row['question_id']);
    }


    // Create empty responses in 'quiz_response' table
    foreach ($questionIDs as $questionID) {
        $query = "INSERT INTO quiz_response (user_id, question_id) VALUES ('" . $_SESSION['user_id'] . "', '" . $questionID . "')";
        mysqli_query($dbc, $query);
    }


    // If form is submitted, update choice_id column of quiz_response table
    if (isset($_POST['submit'])) {
        // Inserting choices into the response table
        foreach ($_POST as $choice_id => $choice) {
            $query = "UPDATE quiz_response SET choice_id = '$choice', answer_time=NOW() " .
            "WHERE response_id = '$choice_id'";
            mysqli_query($dbc, $query);
        }

        // Update the 'is_correct' column
        // Pull all is_correct data from question_choice table relating to specific response_id
        $total_Qs = 0;
        $correct_As = 0;
        foreach ($_POST as $choice_id => $choice) {
            $query = "SELECT qr.response_id, qr.choice_id, qc.is_correct " . 
            "FROM quiz_response AS qr " . 
            "INNER JOIN question_choice AS qc USING (choice_id) " . 
            "WHERE response_id = '$choice_id'"; 
            $data=mysqli_query($dbc, $query);
            // Update is_correct column in quiz_response table
            while ($row = mysqli_fetch_array($data, MYSQLI_ASSOC)) {
                $total_Qs ++;
                if ($row['is_correct'] == 1) {
                    $query2 = "UPDATE quiz_response SET is_correct = '1' " . 
                    "WHERE response_id = '$row[response_id]'";
                    mysqli_query($dbc, $query2);
                    $correct_As ++;
                }
            }   
        }       

        // Update high_score table with $correct_As
        $quiz_id = $_POST['quiz_id'];
        $query = "INSERT INTO high_score " . 
        "VALUES ('0', '" . $_SESSION['user_id'] . "', '" . $quiz_id . "', '" . $correct_As . "', NOW())";
        mysqli_query($dbc, $query);

        // Display score after storing choices in database
        echo 'You got ' . $correct_As . ' out of ' . $total_Qs . ' correct';

        exit();
        mysqli_close($dbc);
    }



    // Grab the question data from the database to generate the form
    $Q_and_Cs = array();
    foreach ($questionIDs as $questionID) {
        $query = "SELECT qr.response_id AS r_id, qr.question_id, q.question " . 
           "FROM quiz_response AS qr " . 
           "INNER JOIN question AS q USING (question_id) " . 
           "WHERE qr.user_id = '" . $_SESSION['user_id'] . "' " .
           "AND qr.question_id = '" . $questionID . "'";
        $data = mysqli_query($dbc, $query) 
            or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query");
        // Store in $questions array, then push into $Q_and_Cs array
        while ($row = mysqli_fetch_array($data, MYSQL_ASSOC)) {
            print_r($row);
            $questions = array();
            $questions['r_id'] = $row['r_id'];
            $questions['question_id'] = $row['question_id'];
            $questions['question'] = $row['question'];
            // Pull up the choices for each question
            $query2 = "SELECT choice_id, choice FROM question_choice " .
            "WHERE question_id = '" . $row['question_id'] . "'";
            $data2 = mysqli_query($dbc, $query2);
            while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM)) {
                $questions[] = $row2[0];
                $questions[] = $row2[1];
            }
            array_push($Q_and_Cs, $questions);
        }
    }
    mysqli_close($dbc);


    // Generate the quiz form by looping through the questions array
    echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
    echo '<h2>' . $quiz_title . '</h2>';
    $question_title = $Q_and_Cs[0]['question'];
    echo '<label for="' . $Q_and_Cs[0]['r_id'] . '">' . $Q_and_Cs[0]['question'] . '</label><br />';
    foreach ($Q_and_Cs as $Q_and_C) {
        // Only start a new question if the question changes
        if ($question_title != $Q_and_C['question']) {
            $question_title = $Q_and_C['question'];
            echo '<br /><label for="' . $Q_and_C['r_id'] . '">' . $Q_and_C['question'] . '</label><br />';
        }
        // Display the choices
        // Choice #1
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[0] . '" />' . $Q_and_C[1] . '<br />';
        // Choice#2
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[2] . '" />' . $Q_and_C[3] . '<br />';
        // Choice #3
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[4] . '" />' . $Q_and_C[5] . '<br />';
        // Choice #4
        echo '<input type="radio" id="' . $Q_and_C['r_id'] . '" name="' . $Q_and_C['r_id'] . '" value="' . $Q_and_C[6] . '" />' . $Q_and_C[7] . '<br />';
    }
    echo '<br /><br />';
    echo '<input type="hidden" name="quiz_id" value"'.$quiz_id.'" />';
    echo '<input type="submit" value="Grade Me!" name="submit" />';
    echo '</form>';
    // echo 'Quiz_id: '.$quiz_id.'<br />';


    // Insert the page footer
    require_once('footer.php'); 

?>
当我用一个数字(即2)代替
$quick\u id
时,它就起作用了,但是为了让脚本适用于不同的测验,我需要能够对不同的测验使用不同的quick\u id


我在使用$\u GET从quizlist.php获取变量,然后在提交表单时将其作为隐藏值传递时遇到问题我做错什么了吗?还是我遗漏了一些非常明显的东西?如果有任何帮助,我将不胜感激!谢谢…

关于第一条线索,在我看来,您正在收到$QUITY\u id表单GET请求(这是正确的),但您有一个条件

if (isset($_POST['submit'])) {

只有在提交表单(POST请求)时才执行,而不是单击链接。因此,当您单击链接时,此条件下的所有代码都不会执行

我根据您的提示编辑了我的代码,但仍未能成功更新“测验id”列。我在测验表单中添加了一个隐藏值,然后尝试使用$\u POST函数访问它。。。还有其他想法吗?请解释脚本的具体工作方式、标记等。当您单击链接时,您只能从提交表单时的$\u-get获取变量-无论是从$\u-get还是从$\u-POST,具体取决于表单元素fyi的方法属性,这取决于遇到此页面的任何用户,我在表单的隐藏输入元素中留下了等号。。。
$query = "INSERT INTO high_score " . 
            "VALUES ('0', '" . $_SESSION['user_id'] . "', '" . $quiz_id . "', '" . $correct_As . "', NOW())";
if (isset($_POST['submit'])) {