Reputation: 2210
Here is the database-design http://www.flickr.com/photos/michaelstitt/6077647604/in/photostream/ that I decided upon -- using the help from my previous post on Stack Overflow.
I am attempting to build a Quiz web application using PHP and MySQL. It seems a lot easier to build an HTML form and code the 'question' and 'question_choices' by hand. However, I'd like to populate the form using data in my database.
So, my question is how do you suggest I pull data from the tables 'question' or 'question_choices' from my database to fill in HTML forms?
The HTML forms will look something like this:
<form method="post" action="quiz1.php">
<b>What was the make/model of my first car?</b>
<br>
<input type="radio" value="0" name="q_1">
Toyota Camry <br />
<input type="radio" value="1" name="q_1">
Honda Accord <br />
<input type="radio" value="0" name="q_1">
Ford Explorer <br />
<input type="radio" value="0" name="q_1">
Plymouth Voyager <br />
Honda Accord will be derived from the 'choice' column in the table question_choices. The value="1" will be determined by the 'is_right_choice' column and will populate the 'is_right' column in the user_question_answer table. Does this seem like a good idea?
Any other advice would be greatly appreciated. Thank you in advance.
Upvotes: 0
Views: 2920
Reputation: 15892
That's not a good idea since anyone looking at the HTML source can figure out which is the right answer. On the other hand you have all the information there in your schema to do this server side (And protect the answers).
Question Time
I'm guessing (you don't say) that you want to show all quiz questions on one page. You'd want your form posting back to a default quiz.php
page. (<form method="post" action="quiz.php" />
)
Because quiz.php
can handle any quiz, you need to embed the quiz ID in the form, so when you're processing results you can see which quiz is being answered. Try <input type="hidden" name="quizid" value="1" />
(assuming this was quiz #1).
You also need to be explicit about the order you pull your questions out of the database since they're switching from the ID to the order (so if you have a quiz of 10 questions, the first one with ID 11, the last with ID 21 they would become question 1-10). This is easily handled by order by id
in your SQL statement.
The output of the question is fine, the radios value should be the ID of the choice from question_choices
.. all values will be unique and it isn't clear from code which is the right answer. I'd suggest renaming the elements to a_<#>
since when you're processing they're actually answers you're getting (not questions), and you might want to consider surrounding the question+choices in a fieldset
(for readability):
<fieldset>
<b>What was the make/model of my first car?</b><br />
<input type="radio" value="214" name="a_1">Toyota Camry <br />
<input type="radio" value="215" name="a_1">Honda Accord <br />
<input type="radio" value="216" name="a_1">Ford Explorer <br />
<input type="radio" value="217" name="a_1">Plymouth Voyager <br />
</fieldset>
At submit time I'd validate that for all questions at least one option is selected (unless no answer is also an answer)... so that a user can't accidentally submit a half filled out form (by pressing enter or because they missed a question).
Answer Time
So first thing in quiz.php
is to catch $_POST["quizid"]
and load up the related questions and question_choices from the database.
Next iterate through each answer (1 -> n where n is the number of questions) and map these into question.ID
values.
Compare the value of each answer: $_POST["a_#"]
(#=1-n) to find out which question_choice
ID the user picked - which can be directly inserted into user_question_answer
.
If you also want to store is_right
in that table you'll have to check these IDs against question_choice
to see if is_right_choice
is set {you're actually double storing this data, you could normalize further by removing is_right
from user_question_answer
since you can already map an answer (by choice_id
) to question_choices
and check whether it was the right one}
And you're done!
Upvotes: 1