gpt4 book ai didi

php - 如何使用 php 验证测验系统中选定的单选按钮

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:08 29 4
gpt4 key购买 nike

我正在准备一个使用 php 和 mysql 的测验应用程序。我在验证单选按钮时遇到问题。

当我点击检查按钮时,它只检查第一个选项是否正确,但实际上它应该检查单选按钮中的所选选项是否正确。

我用过的代码在这里:

$singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' LIMIT 1");
while($row = mysql_fetch_array($singleSQL)){
$id = $row['id'];
$thisQuestion = $row['question'];
$type = $row['type'];
$subject =$row['subject'];
$exam =$row['exam'];
$explan =$row['explan'];
$question_id = $row['question_id'];
$s ='<strong>'.$subject.'</strong>';
$e ='<small>'.$exam.'</small>';
$q = '<h2>'.$thisQuestion.'</h2>';
$ex ='<div id="welcomeDiv" style="display:none;" class="expl" >'.$explan.'</div>';
$sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()")or die(mysql_error());

while($row2 = mysql_fetch_array($sql2)){
$id2=$row2['id'];
$answer = $row2['answer'];
$correct = $row2['correct'];
$answers .= '<table class="table table-hover table-bordered">
<tr>
<td class="chk">
<label style="cursor:pointer;">
<input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label></td>
</tr>
</table>
<input type="hidden" id="qid" value="'.$id.'" name="qid"><br />';

$result=mysql_query("SELECT id FROM answers WHERE question_id='$question' ");
$nrows=mysql_num_rows($result);
for($i=0;$i<=4;$i++){
if (isset($_POST[$correct])) {
$answer= $_POST[$correct];
}

if($answer&&$correct==1){
echo $dv.='<div style="display:none;" class="green" id="chek" >Your answer '.$answer.' is correct</div>';
} else {
echo $dv2.='<div style="display:none;" class="red" id="chek" >Your answer '.$answer.' is worng</div>';}
}

我有两个表格,一个是问题,另一个是答案

问题表

            id question_id question 
1 1 question1

答案表

            id questions_id answer correct
1 1 op1 1
2 1 op2 0
3 1 op3 0
4 1 op4 0

最佳答案

name="rads"是您应该为每组答案设置唯一名称的问题。

你应该添加这样的东西:

$i = 1;
while($row2 = mysql_fetch_array($sql2)){
$id2=$row2['id'];
$answer = $row2['answer'];
$correct = $row2['correct'];
$answers .= '<table class="table table-hover table-bordered">
<tr>
<td class="chk">
<label style="cursor:pointer;">
<input type="radio" name="rads_'.$i.'" value="'.$correct.'">'.$answer.'</label></td>
</tr>
</table>
<input type="hidden" id="qid" value="'.$id.'" name="qid"><br />';
$i++;

关于php - 如何使用 php 验证测验系统中选定的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323067/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com