ifis Posted January 24, 2008 Share Posted January 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/ Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 use arrays, or variable referencing. better to use arrays. Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448037 Share on other sites More sharing options...
revraz Posted January 24, 2008 Share Posted January 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448042 Share on other sites More sharing options...
ifis Posted January 24, 2008 Author Share Posted January 24, 2008 The data is AlphaNumeric, actually just letters. Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448049 Share on other sites More sharing options...
ifis Posted January 24, 2008 Author Share Posted January 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448054 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448062 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 I hate this edit time limit thing. you may have to use " instead of ' on the $_POST["answer$i"] and question $_POST["question$i"] Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448067 Share on other sites More sharing options...
revraz Posted January 24, 2008 Share Posted January 24, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448070 Share on other sites More sharing options...
ifis Posted January 24, 2008 Author Share Posted January 24, 2008 ok, I'm putting it in.. Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448074 Share on other sites More sharing options...
ifis Posted January 24, 2008 Author Share Posted January 24, 2008 You guys are right on the money again. Odviously I'm pretty new to PHP and did not even think about putting a $_POST within a while statement. Thanks again for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448078 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 add some validating, otherwise someone can possibly dump your database. Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448081 Share on other sites More sharing options...
ifis Posted January 24, 2008 Author Share Posted January 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448161 Share on other sites More sharing options...
revraz Posted January 24, 2008 Share Posted January 24, 2008 I would, don't rely on the data being passed is actually from the pulldown. Quote Link to comment https://forums.phpfreaks.com/topic/87599-solved-variables-within-a-while-statement-and-mysql/#findComment-448165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.