无法使用$ _POST传递隐藏的PHP值

时间:2011-10-13 20:49:29

标签: php mysql sql forms

我正在构建一个测验Web应用程序,它可以从我服务器上的数据库动态生成。我在表单字段中使用隐藏输入传递quiz_id的值时遇到问题。这是quiz.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>';


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

?>

代码运行生成表单后,我回显$quiz_id的值并正确显示。但是,当我尝试使用$quiz_id = $_POST['quiz_id'];获取隐藏值并回显它的值时,它不起作用。

所以,它让我相信我没有正确传递这个隐藏的价值。此外,我更新high_score表的INSERT查询适用于每个列,除了我传递$quiz_id的值。虽然它可以工作,但我传给它一个整数。

我的问题是,在发送隐藏变量$quiz_id的过程中我做错了什么?

2 个答案:

答案 0 :(得分:5)

更改

 echo '<input type="hidden" name="quiz_id" value"'.$quiz_id.'" />';

echo '<input type="hidden" name="quiz_id" value = "'.$quiz_id.'" />';

我认为你错过了一个“=”。

修改:另外,您正在使用$_GET隐藏表单字段?你应该这样做:

$quiz_id = $_POST['id'];

答案 1 :(得分:1)

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

这里你从一个不存在的get var声明了$ quiz_id。这不是post var:)