在线问答系统中总结好答案时出现的问题

ryevplcw  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(276)

我想为驾校建立一个在线测验系统。他们要求我提供的问题和答案将被添加到数据库中。我确实做到了,但问题是我很难知道答案是否正确。
我用来生成表单的代码如下:

$sql = mysql_query("SELECT * FROM questions ORDER BY question_id ASC");

                        while($row = mysql_fetch_array($sql)){
                            echo ' 

                            <section>
                            <h1>'.$row['question'].'?</h4>
                                </br> <hr> </br>

                                <div class="row">
                                <div class="col-sm-4 foto pb-2">
<img  src="" style="max-width:100%;height: auto;" >
</div>
   <div class="col-12 col-sm-8">
   <div class="pergjigjet mb-2 pl-2 pr-2" style="padding-left: .5rem!important;padding-right: .5rem!important;margin-bottom: .5rem!important;">
   <div class="pergjigje">
   <label><input type="checkbox" name="q_'.$row['question_id'].'_a_1" value="1"> '.$row['answer1'].'.</label>
   </div>
   <div class="pergjigje">
   <label><input type="checkbox" name="q_'.$row['question_id'].'_a_2" value="1"> '.$row['answer2'].'.</label>
   </div>
   <div class="pergjigje"><label>
   <input type="checkbox" name="q_'.$row['question_id'].'_a_3" value="1"> '.$row['answere'].'.</label>
   </div>
   </div>
</div>
</section>

测验共有30个问题,最多3个正确答案。如果你没有选择所有正确的答案,那就是错的。这意味着如果你没有为问题选择正确的答案,你就得不到分数。
post数据的数组如下所示。

array(4) {
  ["q_1_a_1"]=>
  string(1) "1"
  ["q_1_a_2"]=>
  string(1) "1"
  ["q_2_a_1"]=>
  string(1) "1"
  ["q_2_a_2"]=>
  string(1) "1"
}

table就是这个,

id, 
question_id, 
exam_id, 
question, 
answer1,  
answer2,
answer3,
CorrectAnswers,
QuestionPoints

我以这种格式存储正确答案

1,2

p、 客户要求我使用mysql,而不是mysqli或pdo

7kjnsjlb

7kjnsjlb1#

如果您对答案复选框使用数组样式的名称,并将答案编号放在 value ,例如。

<label><input type="checkbox" name="q_'.$row['question_id'].'[]" value="1"> '.$row['answer1'].'.</label>
   </div>
   <div class="pergjigje">
   <label><input type="checkbox" name="q_'.$row['question_id'].'[]" value="2"> '.$row['answer2'].'.</label>
   </div>
   <div class="pergjigje">
   <label><input type="checkbox" name="q_'.$row['question_id'].'[]" value="3"> '.$row['answer3'].'.</label>
   </div>

当表格提交时, $_POST['q_1'] 将是所有选定答案的数组。您可以使用 implode(",", $_POST['q_1']) ,并将此字符串与表中的正确答案列表进行比较。

相关问题