need help in making quiz
Posted: Fri Oct 09, 2009 9:44 am
hi i want to make an mcq quiz. what i have done sofar is i make mysql table called `quiz` in this table i have following columns
`id, question, option1, option2, option3, option4, correct`. i retrieve all the questions and options from this table through following php code
now when the user selects answer and click on submit then it should go and check the correct answer in the table from column `correct`. if the answer selected by the user is the same as in column correct then increase the user score by 1. i am trying all this in the following code but it is not working. can anyone help me
`id, question, option1, option2, option3, option4, correct`. i retrieve all the questions and options from this table through following php code
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<p>
<form name="form1" method="post" action="quiz2action.php">
<?php
$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);
$queryquestions = "SELECT * FROM quiz" or die();
$resultquestions = mysql_query($queryquestions) or die();
while($rowquestions = mysql_fetch_array($resultquestions))
{
$questionid = $rowquestions['id'];
$question = $rowquestions['question'];
$answerone = $rowquestions['option1'];
$answertwo = $rowquestions['option2'];
$answerthree = $rowquestions['option3'];
$answerfour = $rowquestions['option4'];
echo "
<b>Question $questionid: $question</b><br>
<input type='radio' name='".$questionid."' value='".$answerone."'>".$answerone."<br>
<input type='radio' name='".$questionid."' value='".$answertwo."'>".$answertwo."<br>
<input type='radio' name='".$questionid."' value='".$answerthree."'>".$answerthree."<br>
<input type='radio' name='".$questionid."' value='".$answerfour."'>".$answerfour."<br>";
echo "<br><br>";
}
?>
</p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</form>
<p> </p>
</body>
</html>
Code: Select all
$questionid=(isset($_POST['$questionid']))?$_POST['$questionid']:"";
$answerthree=(isset($_POST['$answerthree']))?$_POST['$answerthree']:"";
$db = mysql_connect("localhost");
mysql_select_db("videoshop", $db);
$query = "SELECT * FROM quiz WHERE id = ".$questionid." and correct = ".$answerthree."";
echo $query;
$result = mysql_query($query);
$score = 0;
if($result)
{
$score++;
}