I have a php quiz script. When i click submit, it successfully outputs the total number of correct answers. but when i submit a query for random select then the result of a quiz is wrong please help me as soon as possible: the code is: <?php include("contentdb.php"); $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); if (!$_POST['submit']) { echo "<form method=post action=$PHP_SELF>"; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; $answer = $row["answer"]; echo "<tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name='q$id' value=\"$opt1\"></td> <td>$opt2 <input type=radio name='q$id' value=\"$opt2\"></td> <td>$opt3 <input type=radio name='q$id' value=\"$opt3\"></td></tr>"; } echo "</table>"; echo "<input type='submit' value='See how you did' name='submit'>"; echo "</form>"; } elseif ($_POST['submit']) { $score = 0; $total = mysql_num_rows($display); while ($result = mysql_fetch_array($display)) { $answer = $result['answer']; $q = "q$result[id]"; $q = trim($q); if ($_POST[$q] == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; } ?> If I add the query as like $display = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT 4",$db); It wouldn't work exactly, such as answer will come wrong. e.g if i can select all of the correct answer but answer come 40% or 50% or other please help me. please link send to facebook.riseuplabs@gmail.com with best regards Bashir
thank you for reply! but it was not work correctly when i use this: $q = "q$result['id']"; the error is folloing: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\newquiz\test5.php on line 41 if i use $q = "q$result[$id]"; then out put is 0 this is the problem when i select question from database randomly.
Print out the response and answer and you should see what is causing the problem. My guess is that the problem is your data, not the code. When you do a regular select, you are always testing the same 4 questions. When you do a random select, you are randomly testing all your question. Some of your answers doesn't match up with the answer options 100%. echo $_POST[$q]; echo $answer; if ($_POST[$q] == $answer) { ... Code (markup):
Thank you jkl6 for your suggestion. Also i have the problem, this is: if i use: echo $_POST[$q]."</br>"; then all selected answer is not render, only show the answer which is correct and for the following: echo $answer."</br>"; showing the answer from database but not the answer of that question which is show before submit. Actually answer from database and selected is come randomly for that score is come randomly. what's the problem I can't find it. Please help me.