如何制作循环语句,以从上一页获取值?

时间:2019-02-13 09:34:23

标签: php html mysql

我正在本地主机上的网站上建立在线测验/考试页面(仅用于教育目的)。但是,在检查答案时,它只会检查第二个问题以找到正确的答案。 例如,有2个问题。我选择了两个正确的答案,但是在下一页显示我从2中得到了1分。

我也尝试过使用POST / GET方法... 但还是一样

参加测验(问题)的学生。php

<?php 

    include("conn.php");
    $quiz_id = intval($_GET['quiz_id']); 
    $display = mysqli_query($con,"SELECT * FROM question WHERE quiz_id = 
    $quiz_id");  


    if (!isset($_POST['Submit']) || ($_POST['Submit'] != 'Mark')) { 

       while ($row = mysqli_fetch_array($display)) {  
          echo "<form method=post action='score.php?quiz_id=";
          echo $row['quiz_id'];
          echo "'>";


          echo "<table border=0>";  



          $id = $row["question_id"];    
          $question = $row["questions"];    
          $opt1 = $row["option1"];    
          $opt2 = $row["option2"];    
          $opt3 = $row["option3"];
          $opt4 = $row["option4"];    
          $answer = $row["answer"];   

          echo "<tr><td colspan=3><br><b>$question</b></td></tr>";    

          echo "
          <tr>
              <td>$opt1 <input type=radio name='q$id' value=\"$opt1\"></td>
              <td>$opt2 <input type=radio name='q$id' value=\"$opt2\"></td>
              <td>$opt3 <input type=radio name='q$id' value=\"$opt3\"></td>
              <td>$opt4 <input type=radio name='q$id' value=\"$opt4\"></td>
          </tr>
          ";

       }     

       echo "</table>";


       echo "<input type='Submit' name='Submit' value ='Mark'>";  
       echo "</form>";    
    }


    ?> 

Score.php(标记结果)

    <?php

    include("conn.php");
    $quiz_id = intval($_GET['quiz_id']); 

    if ($_POST['Submit']) {   

       $display = mysqli_query($con,"SELECT * FROM question WHERE quiz_id = $quiz_id");  
       while ($row = mysqli_fetch_array($display)) {

          $id = $row["question_id"];
          $answer = $row["answer"];
          $score = 0;    
          $total = mysqli_num_rows($display);        

          while ($result = mysqli_fetch_array($display)) {                            


             $answer = "$result[answer]";            
             $q = "q$result[question_id]";                    
             $q = trim($q);
             if ($_POST[$q] == $answer) {                
                $score++;                 
             }        
          }        

          echo "<p align=center><b>You scored $score out of $total</b></p>";    
          echo "<p>";        
       }
    }
    ?>

我希望结果是2分之2。 但它仅显示2分之一。

更新

<?php

include("conn.php");
$quiz_id = intval($_GET['quiz_id']);

//echo 'from form: '.$_POST['q14']; // from form


 if ($_POST['Submit']) {   

$display = mysqli_query($con,"SELECT * FROM question WHERE quiz_id = 
$quiz_id");  

while ($result = mysqli_fetch_array($display)) { 

$id = $result["question_id"];
$answer = $result["answer"];
$score = 0;    
$total = mysqli_num_rows($display);        




$answer = $result['answer'];            
$q = $result['question_id'];   
echo 'answer: '.$answer;

//echo $q;    
//$q = trim($q);
/* if ($q == $answer) {                
$score++;                 

//echo "<p align=center><b>You scored $score out of $total</b></p>";    
//echo "<p>";  

}
 }

?>

0 个答案:

没有答案