2011-10-13 24 views
0

我正在构建一个测验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'); 

?> 

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

因此,它让我相信我没有正确传递这个隐藏的价值。此外,我更新high_score表的INSERT查询适用于每列,除了我传递的值为$quiz_id。虽然它的作品,如果我把它传递给它的整数。

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

+2

看看你的源代码,并确保''是你所期望的。 –

+1

是的,我认为你的源代码中隐藏的元素的'value'后面缺少'='符号。 –

回答

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']; 
+0

哇!我觉得自己像个白痴。感谢您发现! – Abundnce10

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

在这里你宣布从一个不存在的GET变种quiz_id $。这不是post var :)

+0

这些是从quizlist.php脚本传递的,对不起,我没有提到。 – Abundnce10