0

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

                        <form method="post">
                        <?php
                            $question = mysql_query("SELECT * FROM `questions`");
                            $stat = mysql_fetch_assoc($question);
                            $num = mysql_num_rows($question);
                            $questionid = 0;
                            for($i=0;$i<=$num;$i++)
                            {
                                $question = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
                                $stat = mysql_fetch_assoc($question);
                                //if($stat['answer'] == null
                                echo $stat['question'] . '<br />';
                                echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer1']."<br />";
                                echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer2'] . '<br />';
                                echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer3'] . '<br />';
                                echo '<input type="radio" name="a'.$x.'" value="'.$x.'" />' .$stat['answer4'] . '<br />';
                                $questionid++;
                                $x = $x+1;
                            }
                            echo $result;
                        ?>
                        <input type="submit" name="go" value="Go" />
                    </form>

That part basically:

$x = 1;
$question = mysql_query("SELECT * FROM `questions`");
$stat = mysql_fetch_assoc($question);
$num = mysql_num_rows($question);
if(isset($_POST['go']))
{
    for($i=0;$i<$num;$i++)
    {
        if ($_POST['ans'.$x] == $row['correct']) 
        { 
            $result = $result + 1; 
        }
        $x = $x + 1;
    }
}

For some reason it doesn't post the results properly and I think something is wrong with the code, would appreciate help.

Floris
  • 45,857
  • 6
  • 70
  • 122
Michael Naidis
  • 116
  • 2
  • 4
  • 15
  • 1
    did you try any basic debugging? e.g. `var_dump($_POST)`? You seem to be building field names as `a1`, `a2`, etc... but are looking for `ans1`, `ans2`, etc... in your php code. – Marc B Jan 22 '13 at 15:45
  • Can you print out some intermediate results? For example, what is `$row['correct']` - is it what you expect? – Floris Jan 22 '13 at 15:46
  • I think Marc is onto something with the difference between `a.` and `ans.` – Floris Jan 22 '13 at 15:47
  • I changed the ans to a in the PHP part with the result checks, $_row['correct'] is basically an integer which tells which answer is correct. – Michael Naidis Jan 22 '13 at 15:49
  • Does it matter if $result has never been initialized? I don't know if it is outside the code, but here I just see result getting incremented and displayed with no value initially set. – Walls Jan 22 '13 at 15:50
  • No, result is 0 right now. Everytime I select answers, doesn't matter which, I get "3" echoed at $result. – Michael Naidis Jan 22 '13 at 15:52
  • That-s the var dump outside the loop: array(2) { ["a3"]=> string(1) "3" ["go"]=> string(2) "Go" } – Michael Naidis Jan 22 '13 at 16:19
  • Where do you assign a value to `$row`? – Floris Jan 22 '13 at 16:40
  • I've assigned a value now but still not working, now $result echoes nothing. – Michael Naidis Jan 22 '13 at 16:54
  • I saw that it's possible to make it with a while loop, can someone show me how? – Michael Naidis Jan 22 '13 at 17:00
  • By the way - you initialize $x to 1 when checking the answers, but I think the first question is numbered zero... Still I think there's a problem in that you don't appear to be iterating through the questions - see my proposed solution as answer below. – Floris Jan 22 '13 at 17:13
  • When exactly is the second block of code called - it's not obvious from the snippets you posted. I have been presuming it's a separate block of code after the user has finished answering all the questions - but I'm missing some statements that show this flow. – Floris Jan 22 '13 at 17:23
  • It's called at the top of the file. – Michael Naidis Jan 22 '13 at 20:04

1 Answers1

0

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!

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Even if I pick the correct answer it tells '0', maybe it's something with the MySQL: The structure is: id question_id question type correct answer1 answer2 answer3 answer4 ID is AI and answer1,answer2,etc'...'s value is an integer (1,2,3,4) and it specifies the answer he picked. The text of the answers are in answer1,answer2,answer3,answer4 fields. – Michael Naidis Jan 23 '13 at 12:26
  • Alright, finally managed to fix it! Just added $_POST['a'.$x] value and then compared it, which gave the right answer – Michael Naidis Jan 23 '13 at 12:37
  • OK - glad you got it working. In my mini database the correct answer (text, not index) was in the "answer" column... If this solution works you might consider "accepting" it... – Floris Jan 23 '13 at 12:44