You are on page 1of 1

<?

php
/* quizer.php */
if( $_SERVER['REQUEST_METHOD']=='POST' ){
/* select correct answers from db */
$sql='select * from `quiz`';
$res=mysql_query( $sql, $con );

/* store answers in an array for marking */


$answers=array();
$score=0;
$index=0;

/* populate the answers array */


while( $rs=mysql_fetch_object( $res ) ){
$answers[]=$rs->correct_answer;
}

/* mark the answers */


foreach( $_POST as $question => $answer ){
echo $question.' '.$answer;
if( $answer === $answers[ $index ] ) $score++;
$index++;
}
/* update the db */
$sql="update `user` set `score`=$score where `username`='$username';"; /*
etc */
}
?>

<form action="quizer.php" method="post">


<!-- assume select menus for questions and possible answers -->
<label for='q1'>what is your name?
<select name='q1'>
<option value="a">rolex</option>
<option value="b">wales</option>
<option value="c">israel</option>
<option value="d">ade</option>
</select>
</label>

<br />

<label for='q2'>is nigeria a country?


<select name='q2'>
<option value="a">no</option>
<option value="b">yes</option>
</select>
</label>

<br />

<input type="submit" value='Submit Quiz'>


</form>

You might also like