Jump to content

[SOLVED] Variables within a while statement and mysql


ifis

Recommended Posts

I created a quiz, when it is submitted, I want to correct it using a loop, unfortunatly, the code I wrote is not working.

$limit=$_POST['limit']; 
$question1=$_POST['question1'];
$answer1=$_POST['answer1'];
$question2=$_POST['question2'];
$answer2=$_POST['answer2'];
$question3=$_POST['question3'];
$answer3=$_POST['answer3'];
$i=1;
while ($i<=$limit){
$sql="SELECT answer FROM writtenqs WHERE numquestion='$question$i'";
$result=mysql_query($sql);

$row = mysql_fetch_array($result);
if ($row[answer]==$answer$i)
{
$correct++;
    $qs++;
    echo "Question $i was correct.";
}
else
{
    $qs++;
    echo "Question $i was wrong.";
}
$i++;
}
echo "Your score is $correct/$qs.";

Am I just missing something, or is this not the correct way to do it.  I have tried the code with actual numbers, i.e. question1,answer1 and it worked correctly.  Thanks

What type of data is it? Numeric or AlphaNumeric?

 

This line

 

if ($row[answer]==$answer$i)

 

should probably change to

 

if ($row['answer']==$answer$i)

 

Also, echo your querry and check for errors.

 

I tried adding in the quotation marks

$row = mysql_fetch_array($result);
if ($row['answer']=='$answer$i')
{

I had to add quotes around $answer$i because it gave me an error.  I think the correct handeling might be '$answer'.'$i' to combine the two variables, I have not had to do this before though

I created a quiz, when it is submitted, I want to correct it using a loop, unfortunatly, the code I wrote is not working.

$limit=$_POST['limit']; 
$i=1;
while ($i<=$limit){
$sql="SELECT answer FROM writtenqs WHERE numquestion='".$_POST['question$i']."'";
$result=mysql_query($sql);

$row = mysql_fetch_array($result);
if ($row[answer]==$_POST['answer$i']
{
$correct++;
   $qs++;
   echo "Question $i was correct.";
}
else
{
   $qs++;
   echo "Question $i was wrong.";
}
$i++;
}
echo "Your score is $correct/$qs.";

Am I just missing something, or is this not the correct way to do it. I have tried the code with actual numbers, i.e. question1,answer1 and it worked correctly. Thanks

What error?

 

And yes, use . to merge them as one.

 

I tried adding in the quotation marks

$row = mysql_fetch_array($result);
if ($row['answer']=='$answer$i')
{

I had to add quotes around $answer$i because it gave me an error.  I think the correct handeling might be '$answer'.'$i' to combine the two variables, I have not had to do this before though

This is a little off topic, but I just check the last post while working.  If I use radio buttons or a drop down menu, do I need additional form validating as long as I am using the $_POST method?

add some validating, otherwise someone can possibly dump your database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.