Given this table in my database:
|AnswerID|QuestionID|AnswerText|
|21 |2 |User |
|22 |2 |Admin |
|23 |2 |Guest |
|24 |2 |User2 |
I have created a quiz using PAGINATION to display individual question and its options on each of the page. Example code snippet is as shown below:
$questionID=0;
while ($list = mysql_fetch_assoc($result))
{
echo $list['QuestionID'] . ":" . $list['QuestionText'] . "<br/>";
$questionID=$list['QuestionID'];
}
$optionsquery="SELECT AnswerText,AnswerID FROM Options Where QuestionID=".$questionID;
$optionsresult=mysql_query($optionsquery) or die ('Query failed:'. mysql_error());
while($row = mysql_fetch_array($optionsresult))
{
echo "<br>";
echo "<input type='radio' id='radio-" . $row["QuestionID"] . "-"
. $row["AnswerID"] . "' name='" . $row["QuestionID"]
. "' value='" . $row["AnswerID"] . "' />
<label for='radio-" . $row["QuestionID"] . "-" . $row["AnswerID"] . "'>"
. $row["AnswerText"] ."</label><br/>";
echo "<br>";
}
Here is my objective. Given this relationship AnswerID<->AnswerText,
I want to store the selected radio chosen by the user into my database table of the following format :
|StudentID|QuestionID(e.g: 1)|QuestionID(e.g: 2)|
|678D |AnswerID(e.g: 11) |AnswerID(e.g: 22) |
|681D |AnswerID(e.g: 14) |AnswerID(e.g: 23) |
As shown in the above,I am not hard-coding the questions and options for each page,they are rather taken from my database of tables. All the codes are being written in only one single PHP file. I have googled (on PHP) but I am confused on how to go about achieving my objective. Any code(PHP) snippets will be helpful as I am at loss of ideas. Thanks in advance.
I would suggest you don’t store it in that way (ie one question per column) …. You would be better creating a table that linked
student,questionandanswer:This gives you better flexibility – you can increase the number of questions easily for example.
To make processing a submitted form easier I would change the HTML output to this :
This would produce inputs as an array which would maan processing them when submitted like this :
You would need to change the
answertableto the name of the table you are using to record answers. And$studentwould be obtained from the current session (im guessing thats where you are storing it)