comparing form submitted data to database data

I am struggling here trying to set up a quiz.

The user submits his 5 questions and I can’t sort out how to compare that to the database.

There are 4 tables

members
answers (has a field isCorrect that lets me know if its the right answer)
questions
quiz (has a field called “score” I just want the number of answers they got correct and memberId)

my query is: SELECT * FROM answers WHERE isCorrect = ‘TRUE’

This pulls in the right answers.

but how in the world do I compare those answers to the members answers??

$_POST[‘quizQuestion-1’];$_POST[‘quizQuestion-2’] …

and then submit that to the database?

I really appreciate any help you experts can lend. Thanks!!

anyone?

A quick Google search reveals:

That one doesn’t do exactly what you want as far as answers however you can probably modify it to fit your needs.

Online Quiz Code Example
http://www.configure-all.com/fusebox.php

Talks about possibilities of quizzes and how to make them fast

This appears to pertain to what you want
http://bytes.com/topic/php/answers/522196-compare-form-data-database

Google Search
http://www.google.com/search?q=comparing+data+in+php&sourceid=ie7&rls=com.microsoft:en-us:IE-Address&ie=&oe=&rlz=1I7GGLL_en#hl=en&sugexp=ldymls&xhr=t&q=comparing+data+from+a+database+and+form+php&cp=40&qe=Y29tcGFyaW5nIGRhdGEgZnJvbSBhIGRhdGFiYXNlIGFuZCBmb3JtIHBocA&qesig=o8HEhcwrmUhkST30b39Eww&pkc=AFgZ2tkeZiQYCQl2FbXtc96o1gcPfzF21qO0KGM-AnTucx848qkoGWZJXnEbA6_Dz5fEKlj-lQJCQQ-TI7xSp12fPTf54SErfw&pf=p&sclient=psy&rls=com.microsoft:en-us%3AIE-Address&rlz=1I7GGLL_en&aq=f&aqi=&aql=&oq=comparing+data+from+a+database+and+form+php&pbx=1&fp=abd5fec803d17261

I assume that the quiz is objective type… so here is the simple set up for it

step-1

the database table for questions can be like the following

question_id int primary key
question text
opta text
optb text
optc text
optd text
correctanswer int // it stores the correct answer’s option position (1 if opt a is correct, 2 if optb is… like that)

step - 2

In the quiz program form design, while displaying the questions, i assume u give options in radio buttons right? so, in the value attribute of the answers, u can give the position numbers as values …i.e., value=“1” for opt-a, and display the text from table… value “2” for opt-b and like wise.

so, when user selects any one option… you would get the position of the answer he chose.
Mind you, u dont have to get the answer text on the whole coz, he is not typing it… the answer is already there the user is just choosing it. so, u only need to know which one he chose and not the total answer itself.

step-3

when the user submits the quiz form, now in the post data, u will have question id and its compared answer given by the user…

now, u can do something like this

foreach($question_id as $q)
{
$rs = mysql_query(“select correctanswer from questions where questionid=”.$q);
$row = mysql_fetch_assoc($rs);
if($row[‘correctanswer’]==$answergivenbyuser)
// do something…
}

this can give u an idea on how it can be done… sure this is not a copy-paste-ready solution…though.

regards,

Thank you so much for pointing me in the right direction!

you are welcome.

glad it helped.

Sponsor our Newsletter | Privacy Policy | Terms of Service