PHP MySQL Quiz
Moderator: General Moderators
PHP MySQL Quiz
I had setup a quiz database which has around 50 questions in it with answers. I need to display one question at a time from database with next and previous button. Can anyone suggest how to start ? i want one question with for options to display in one page and when user clicks the next question shoud come.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP MySQL Quiz
You can google 'Retrieving information from a SQL database using php' or search this forum; The principles behind accessing the database (and getting the information) is the same for most php applications so you're likely to find some good resources
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: PHP MySQL Quiz
Using the below code i manage to display one questions at a time. Can any one suggest how to manage the answers that user selects ?
Code: Select all
<?php
if(isset($_POST['next'])){
$a=$_POST['a'];
}
if(!isset($a)){
$a=0;
}
include('common/connect_quiz.php');
$sql1="SELECT * FROM questions WHERE quiz_details_Id='1' LIMIT 1 OFFSET $a";
$result=mysql_query($sql1);
echo "<form method='post' action=''>";
while ($row = mysql_fetch_array($result))
{
echo $row['question']. "<br/>";
echo "<input type='radio' value='1' name='answer'>" .$row['opt_1']. "<br/>";
echo "<input type='radio' value='2' name='answer'>" .$row['opt_2']. "<br/>";
echo "<input type='radio' value='3' name='answer'>" .$row['opt_3']. "<br/>";
echo "<input type='radio' value='4' name='answer'>" .$row['opt_4']. "<br/>";
}
$a=$a+1;
echo "<input type='hidden' value='$a' name='a'>";
echo "<input type='submit' name='next' value='next'> ";
echo "</form>";
?>
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP MySQL Quiz
Assuming that you have the answers written in the database, you have to compare the answer given by the user with the value written in the database. Do you tie the question to the database with the value in $a?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: PHP MySQL Quiz
Yes my table design is as follows
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| quiz_title | varchar(64) | NO | | NULL | |
| quiz_description | varchar(100) | NO | | NULL | |
+------------------+--------------+------+-----+---------+----------------+
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| question_id | int(11) | NO | PRI | NULL | auto_increment |
| quiz_details_id | int(11) | NO | MUL | NULL | |
| question | varchar(100) | YES | | NULL | |
| opt_1 | varchar(60) | YES | | NULL | |
| opt_2 | varchar(60) | YES | | NULL | |
| opt_3 | varchar(60) | YES | | NULL | |
| opt_4 | varchar(60) | YES | | NULL | |
| opt_ans | int(4) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| quiz_title | varchar(64) | NO | | NULL | |
| quiz_description | varchar(100) | NO | | NULL | |
+------------------+--------------+------+-----+---------+----------------+
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| question_id | int(11) | NO | PRI | NULL | auto_increment |
| quiz_details_id | int(11) | NO | MUL | NULL | |
| question | varchar(100) | YES | | NULL | |
| opt_1 | varchar(60) | YES | | NULL | |
| opt_2 | varchar(60) | YES | | NULL | |
| opt_3 | varchar(60) | YES | | NULL | |
| opt_4 | varchar(60) | YES | | NULL | |
| opt_ans | int(4) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP MySQL Quiz
Code: Select all
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| question_id | int(11) | NO | PRI | NULL | auto_increment |
| quiz_details_id | int(11) | NO | MUL | NULL | |
| question | varchar(100) | YES | | NULL | |
| opt_1 | varchar(60) | YES | | NULL | |
| opt_2 | varchar(60) | YES | | NULL | |
| opt_3 | varchar(60) | YES | | NULL | |
| opt_4 | varchar(60) | YES | | NULL | |
| opt_ans | int(4) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: PHP MySQL Quiz
No they are to store the options. ie
For Example :
What is phishing? (question)
1 ) A computer virus (opt_1)
2 ) A corrupt website (opt_2)
3) A distribution of spam (opt_3)
4 ) An identity theft scheme (opt_4)
Ans : 4) (opt_ans)
For Example :
What is phishing? (question)
1 ) A computer virus (opt_1)
2 ) A corrupt website (opt_2)
3) A distribution of spam (opt_3)
4 ) An identity theft scheme (opt_4)
Ans : 4) (opt_ans)
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP MySQL Quiz
My bad 
I'd recommend a separate table for the answers; it would have an id, question_id (this acts as a foreign key) and opt_ans (containing the answer)
Use this as guidance for creating your own script
Hth
I'd recommend a separate table for the answers; it would have an id, question_id (this acts as a foreign key) and opt_ans (containing the answer)
Code: Select all
<?php
// $question id
$qID = $_POST['a'];
// SQL query
$qry = "SELECT COUNT(`id`) FROM `question_tbl` WHERE `question_id` = '" . $qID . "' AND `opt_ans` = '" . $_POST['answer'] . "' ";
$sql = mysql_query($qry);
$ary = mysql_fetch_array($sql);
$rows = $ary[0];
if ($rows == 1) {
// question answered correctly
}
else {
// wrong answer
}
?>
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: PHP MySQL Quiz
Thanks a lot !
I had made changes in database design. Now my proble is to shuffel or show the questions in a random order. But when i try order by rand() the questions are repeating . Can any one suggest what to do ???? Below is my code...
I also tried
But the questions are repeting 
I had made changes in database design. Now my proble is to shuffel or show the questions in a random order. But when i try order by rand() the questions are repeating . Can any one suggest what to do ???? Below is my code...
Code: Select all
$sql1="SELECT * FROM questions WHERE quiz_details_Id='2' LIMIT 1 OFFSET $a";
$result=mysql_query($sql1);
echo "<form method='post' action=''>";
while ($row = mysql_fetch_array($result))
{
$question_id=htmlentities($row['question_id']);
$quiz_id=htmlentities($row['quiz_details_id']);
$qry = "SELECT * FROM `answers` WHERE que_id='$question_id'";
$sql = mysql_query($qry);
$ary = mysql_fetch_array($sql);
$real_answer = $ary[2];
echo "<h1 class='header'>".$b.") ".$row['question']."</h1>";
echo "<input type='radio' value='1' name='answer'>" .htmlentities($row['opt_1']). "<br/><br/>";
echo "<input type='radio' value='2' name='answer'>" .htmlentities($row['opt_2']). "<br/><br/>";
echo "<input type='radio' value='3' name='answer'>" .htmlentities($row['opt_3']). "<br/><br/>";
echo "<input type='radio' value='4' name='answer'>" .htmlentities($row['opt_4']). "<br/><br/>";
}
I also tried
Code: Select all
$sql1="SELECT Distint * FROM questions WHERE quiz_details_Id='2' ORDER BY RAND() LIMIT 1 OFFSET $a"; Re: PHP MySQL Quiz
There's no real point using DISTINCT with LIMIT 1; you can't possibly get duplicate values if you're only returning a single row. The obvious solution would be to have multiple questions in the same form. Barring that, you could store the ID of the questions already asked in session data and add a clause to your query WHERE id NOT IN (id_of_asked_question)
Re: PHP MySQL Quiz
No i want only one question at a time ! Can we shuffel or change the order of questions retriving by diffrent users. Now all users will get same pattern.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP MySQL Quiz
What about selecting a random number from an array and depending on that return the corresponding question; The random value is used for the question id:
You would have to check that once a value is selected it cannot be used again; this will stop question appearing multiple times.
Code: Select all
$qry = "SELECT fields FROM table WHERE questionID = '" . $randomQuestionIDValue . "' ";
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: PHP MySQL Quiz
Yes, I already explained how to do that. Order by random, and use session data to keep track of questions they've already had. Easy peasy.nithinkk wrote:No i want only one question at a time ! Can we shuffel or change the order of questions retriving by diffrent users. Now all users will get same pattern.
Re: PHP MySQL Quiz
Your solution is not clear for me....Can you tell what should i do in the code ?
Should i create a session array and insert the id in to it and check whether the id is used or not ? But i dont know how to do it ! Can you tell me what to do ?
Code: Select all
//quiz_details_id is same for all questions..in this case 1 is the id.
$sql1="SELECT * FROM questions WHERE quiz_details_Id='1' order by rand() LIMIT 1 OFFSET $a";
$result=mysql_query($sql1);
echo "<form method='post' action=''>";
while ($row = mysql_fetch_array($result))
{
$question_id=htmlentities($row['question_id']);
$quiz_id=htmlentities($row['quiz_details_id']);
$qry = "SELECT * FROM `answers` WHERE que_id='$question_id'";
$sql = mysql_query($qry);
$ary = mysql_fetch_array($sql);
$real_answer = $ary[2];
echo "<h1 class='header'>".$b.") ".$row['question']."</h1>";
echo "<input type='radio' value='1' name='answer'>" .htmlentities($row['opt_1']). "<br/><br/>";
echo "<input type='radio' value='2' name='answer'>" .htmlentities($row['opt_2']). "<br/><br/>";
echo "<input type='radio' value='3' name='answer'>" .htmlentities($row['opt_3']). "<br/><br/>";
echo "<input type='radio' value='4' name='answer'>" .htmlentities($row['opt_4']). "<br/><br/>";
}
Should i create a session array and insert the id in to it and check whether the id is used or not ? But i dont know how to do it ! Can you tell me what to do ?