P.S. I am new to php & webform scripting;
Currently I have the following multiple choice question table inside MySQL database :
|MCQ_ID|Question|Opt_1|Opt_2|Opt_3|Opt_4|Opt_5|
|1|What is the port|23|21|22|80|53
My objective is to create a quiz in a web form, using php scripting where
(1) The Questions are selected from the database & displayed in a multiple choice form with only a single answer to each question
(2) Each webform should display only one question as there will be a timer to record the amount of time spend at a particular question.
Below is my begining script & how should I go from here to achieve my above objectives?
<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#wrapper {
width:950px;
height:auto;
padding: 13px;
margin-right: auto;
margin-left: auto;
background-color: #fff;
}
</style>
</head>
<body bgcolor="#e1e1e1">
<div id="wrapper">
<center><font fact="Andalus" size="5">Test Quiz</font></center>
<br /><br /><br /><br />
</div>
<?php
//Start Variables
$username = "root";
$password = "";
$database = "Test";
//End Variables
//Connect To Database
$link = mysql_connect(localhost,$username,$password) or die ('Could not connect :'. mysql_error());
mysql_select_db($database) or die( "Unable to select database");
//SQL Get Questions
$query = "SELECT * FROM MCQuestionBank";
$result = mysql_query($query) or die ('Query failed:'. mysql_error());
$row = mysql_fetch_array($result,MYSQLI_ASSOC);
//Get results
/*
while ($row = mysql_fetch_array($result,MYSQLI_ASSOC))
{
echo '<br> QuestionName : ' .$row{'Question'} ;
echo '<br> Option 1 : ' .$row{'MCQ_Opt1'};
echo '<br> Option 2 : ' .$row{'MCQ_Opt2'};
echo '<br> Option 3 : ' .$row{'MCQ_Opt3'};
echo '<br> Option 4 : ' .$row{'MCQ_Opt4'};
echo '<br> Option 5 : ' .$row{'MCQ_Opt5'};
}
*/
mysql_free_result($result);
mysql_close($link);
?>
</body>
</html>
That table structure is going to be unmanageable. If you’re going to use a database you need to normalize it. Questions go in one table. Answers in another. That way is much more flexible. What are you are going to do when, for example, a question needs to have more (or less than) 5 choices?
Get some general background on relational databases and then come back to the problem. This might be a good start:
http://www.deeptraining.com/litwin/dbdesign/FundamentalsOfRelationalDatabaseDesign.aspx