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

answers (has a field isCorrect that lets me know if its the right answer)
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!!


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

Talks about possibilities of quizzes and how to make them fast

This appears to pertain to what you want

Google Search

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


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.


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);
// do something…

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


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