3

我正在使用 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
4

1 回答 1

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++;
于 2013-08-20T13:52:55.483 回答