在网上问答系统上总结好答案时出现的问题



我正在为一所驾校构建一个在线测验系统。他们要求我将问题和答案添加到数据库中。我确实做到了,但问题是我很难知道答案是否正确。

我用来生成表单的代码是:

$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个正确答案。如果你没有选择所有正确的答案,那肯定是错的。这意味着如果你没有为问题选择正确的答案,你就得不到分数。

帖子数据的数组是这样的。

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"
}

桌子是这个,

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

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

1,2

p.S客户要求我使用mysql,而不是mysqli或PDO

如果您使用数组样式的名称作为答案复选框,并将答案编号放在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'])的字符串,并将该字符串与表中的正确答案列表进行比较。

最新更新