为什么单选按钮不能正常工作

时间:2017-04-23 20:27:09

标签: php mysql

question{
question_id,
option1,
option2,
option3,
option4,
answer
}

exam_paper{
exam_paper_id,
exam_paper_name
}

exam_question_list{
id,
exam_paper_id,
question_id
}

applicant_do_exam{
applicant_do_exam_id,
exam_paper_id,
question_id,
app_answer
}

I want to display the questions in exam_paper_id=1

$query="SELECT * FROM exam_question_list e LEFT JOIN question q ON e.question_id=q.question_id where exam_paper_id='$exid'";
$result=mysqli_query($dbcon,$query);

<form action="exampaper_result.php?id=<?php echo $exid; ?>&stime=<?php echo $stime?>" method="post">

<table>
<!--question_1-->
<?php 

        $i=1;
        while($row = mysqli_fetch_array($result)){

?>
  <tr>


     <tr  id="exquestion" data-label="QuestionID"><td><input type="hidden" name="question_id" value="<?php echo $row['question_id']; ?>" /></td></tr>
     <tr  id="exquestion" data-label="Question"><td><span><?php echo $i++; ?>)&nbsp;&nbsp;</span><?php echo $row['question']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer" value="1"/><?php echo $row['option1']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer" value="2"/><?php echo $row['option2']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer" value="3"/><?php echo $row['option3']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer" value="4"/><?php echo $row['option4']; ?></td></tr>  

 </tr>


<?php
    } 
    ?>

  </table>

  <input name="submit" type="submit" id="Submit" value="Submit"/>

  </form>

在这里,我可以正确地得到问题和选项。 但我不能在每个问题中选择单选按钮(答案)。 给出的整个页面只为所有问题选择一个单选按钮。

我的错误是什么?

1 个答案:

答案 0 :(得分:0)

您需要分解每个部分的名称,以便在其上下文中可以选择它们。例如name="app_answer[<?php echo $i; ?>]",它将为您提供$_POST['app_answer'] = array()

像这样:

while ($row = mysqli_fetch_array($result)) {
    $g = $row['question_id'];
?>
      <tr id="exquestion" data-label="Question"><td><span><?php echo $i++; ?>)&nbsp;&nbsp;</span><?php echo $row['question']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer[<?php echo $g; ?>]" value="1"/><?php echo $row['option1']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer[<?php echo $g; ?>]" value="2"/><?php echo $row['option2']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer[<?php echo $g; ?>]" value="3"/><?php echo $row['option3']; ?></td></tr>
      <tr data-label="Question"><td><input id="exoption" type="radio"  name="app_answer[<?php echo $g; ?>]" value="4"/><?php echo $row['option4']; ?></td></tr>  
<?php } /*end while*/ ?>

提交结果

<?php
$answers = (array) $_POST['app_answer'];
//type-casted to ensure empty value is still a blank array
foreach ($answers as $question_id => $answer) {
   echo 'Answer to Question - ' . $question_id . ': ' . $answer . '<br/>';
}
exit;

您需要能够将密钥$g / question_id解析为所需的格式。