PDA

View Full Version : id field missing the continuity


chinni
05-21-2003, 01:37 PM
hello,
this is a simple question and answer script sort of multiple choice.
When the user submits the answer for the first question, i show if the answer is correct or wrong and then proceed to next question.Now, the user submits the answer and now, i see the answer is displaying for the first one rather than the 2nd one and when i click on next question, i get the second question again rathern than the 3rd question.In total there are 5 questions in my table and i see the id field is missing the continuity.You can see the working example HERE (http://inkaytown.f2o.org/spiele/quizpage2.php)

Could somebody please tell me where the error would be.

Thanks in advance
PHP:


<?php

include("contentdb.php");

$id = $HTTP_GET_VARS["id"];

if ($id == "")
{
$id = "0";
}

$display = mysql_query("SELECT * FROM $table WHERE id > $id ORDER BY id ASC LIMIT 1 ",$db);

if (!$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=\"hidden\" name=\"id\" value=\"$id\">";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";

}

elseif ($submit)

{

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";

$display = mysql_query("SELECT * FROM $table WHERE id > $id ORDER BY id ASC LIMIT 1 ",$db);
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

echo "<tr><td><br>$question</td></tr>";
if ($$q == $answer)
{
echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
$id = $id+1;
echo("<a href=\"quizpage2.php?id=$id\">Next question</a>");
}

elseif ($$q == "")
{
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
$id = $id+1;
echo("<a href=\"quizpage2.php?id=$id\">Next question</a>");

}

else {
echo "<tr><td>&raquo;you answered ${$q},which is wrong. The answer is $answer</td></tr>";
$id = $id+1;
echo("<a href=\"quizpage2.php?id=$id\">Next question</a>");

}

}
echo "</table></p>";

}

?>

ASAAKI
05-21-2003, 02:30 PM
well this looks to me like the cause of the problem:

<?php
include("contentdb.php");

.
.
.
.
.

elseif ($submit)

{

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";

$display = mysql_query("SELECT * FROM $table WHERE id > $id ORDER BY id ASC LIMIT 1 ",$db);
while ($row = mysql_fetch_array($display)) {

.
.
.
.


you're selecting IDs greater than the current id, so if the current id is 0, it's going to display 1. should probably "WHERE id = $id".

chinni
05-21-2003, 02:51 PM
well, when i change my query, i dont get the answer displayed after the first question is submitted.

Any other ideas please

thanks

ASAAKI
05-21-2003, 08:42 PM
oh ok sorry, i just noticed that your "if (!$submit)" uses the same query for displaying the questions, so that can't be the problem.
hmmmm...let's see. what's happening is this:

1.view question (id=1)
2.submit, and view answer (id=1)
3.go to next question (id=2)
4.submit, and view answer, but the id goes back to id=1, so u end up on the previous answer ...

that means there must be something wrong in the second half of the script, where $submit evaluates to TRUE. looks like the error is happening where you make the 'Next question' link.
The problem might be that while, right at the beginning of the script you've asked for the 'id' value from the form :$id = $HTTP_GET_VARS["id"];, you haven't done anything about getting the id when it's coming from the query string rather than from the form, ie, after clicking on 'Next question' rather than on submitting an answer. So even though you do increase the id before adding it to the query string, the id that actually gets used is the old one, because you never requested the new id that was sent in the query string.
(If this were really the only problem though, you shouldn't have been able to get to the second question either, you should have always been stuck on the first question....:confused: )

anyway, no harm in trying. try this at the beginning, or whatever your version of PHP needs to get querystring values:

$id=$_REQUEST['id'];


actually, i suggest you kind of rearrange your whole code. do it something like this:

<?php

include("contentdb.php");

if(!$submit){

$id = $_REQUEST['id']; // well i'd do this to access querystring variables in my version, as your version is apparently older, you might have to use $HTTP_SERVER_VARS or something...

/*do the query and all the stuff to display the form
.
.
.
*/

if($submit)
$id = $HTTP_GET_VARS["id"];

/* do the stuff to display answer results
.
.
.
.*/