How can I see if the user's choice in the quiz is correct?

前端 未结 1 1387
醉酒成梦
醉酒成梦 2020-12-20 10:25

I am trying to check if the answer that the user chose was correct, but it doesn\'t work properly:

                        
相关标签:
1条回答
  • 2020-12-20 10:51

    Here's another attempt at helping you.

    I actually wrote a "complete solution", and in the process discovered a few little bugs in your code - plus some things I just couldn't understand.

    Principal bug: all your radio buttons have the same value ($x), so no matter what button you press for question 1, the answer is "1", etc. There were other things you did that I could not quite figure out - so what I did instead was create a simple flow - one page that asks the questions, and another that evaluates the results.

    Question page (I obfuscated access parameters for my database - no, I don't use "password" as my password!):

    <html>
    <body>
    <form action="./evaluate.php" method="post">
    <?php
    $server = mysql_connect ('localhost', 'username, 'password');
    mysql_select_db("questionnaire", $server);
    
    $question = mysql_query("SELECT * FROM `Questions`;");
    $x = 0;
    while ($row = mysql_fetch_assoc($question))
    {
       echo $row['question'] . '<br />';
       echo '<input type="radio" name="a'.$x.'" value=1 />' .$row['answer1'] . '<br />';
       echo '<input type="radio" name="a'.$x.'" value=2 />' .$row['answer2'] . '<br />';
       echo '<input type="radio" name="a'.$x.'" value=3 />' .$row['answer3'] . '<br />';
       echo '<input type="radio" name="a'.$x.'" value=4 />' .$row['answer4'] . '<br />';
       $x = $x + 1;
    
    }
    mysql_close($server);
    ?>
    
    <input type="submit" name="Submit" value="Submit" />
    <br>
    </form>
    </body>
    </html>
    

    and evaluate.php: EDIT: I changed the code a bit to make the output "cleaner", and add a red/green touch to show questions that had been answered correctly and incorrectly. Obviously you can take these things much further if you want...

    <html>
    <body>
    
    <?php
    $server = mysql_connect ('localhost', 'username', 'password');
    mysql_select_db("questionnaire", $server);
    
    $question = mysql_query("SELECT * FROM `Questions`;");
    $x = 0;
    $score = 0;
    while ($row = mysql_fetch_assoc($question))
    {
        echo $row['question'] . '?<br />';
    
        $answered = $row['answer'.$_POST['a'.$x]] ;
        $correct = $row['correct'] ;
    
        if ($answered == $correct ) {
            $score++;
            $acolor = 'green' ;
        }
        else {
            $acolor = 'red' ;
        }
    
        echo 'you answered <font color=' . $acolor . '>' . $answered . '<font color=black> <br />';
    
    
        echo 'the correct answer was ' . $correct . '<br />' ;
        echo '-------------------------------------- <br />' ;
    
        $x = $x + 1;
    }
    echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';
    mysql_close($server);
    ?>
    
    </body>
    </html>
    

    This produced (for a simple three-question multiple choice I made) the expected results. Let me know if it works for you!

    0 讨论(0)
提交回复
热议问题