I have 10 question at a time on a page quiz.html- i.e
question 1: what is your name?
<option value="a">a</option>
<option va..>b</option>...
<option va...>e</option>
question 2: what are the colors in usa flag?
<option>a</option>
and so on.
My question is how do i submit this question at a time and add +1
into the score field in the database for each correct answer?
I can do the saving and scoring if there is only 1 question per page i.e
select * from quiz where id='qid'
then
while($fetch=mysql_fetch_row($query)){
if( $fetch["answer"]==$useranswer ){ add +1 to his score; }
}
But for many question i don't know what to do.
The structure of the html is:
<form action="quizer.php" method="post">
<input type="hidden" name="q1" value="qid"> what is your name?
<option value="a">rolex</option>
<option value="b">wales</option>
<option value="c">israel</option>
<option value="d">ade</option>
<br>
<input type="hidden" name="q2" value="qid">
is nigeria a country?
<option value="a">no</option>
<option value="b">yes</option>
and so on...
<input type="submit">
</form>
Assuming that the questions are using select menus as you sort of suggested then this should help - though it is not tested...
<?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>