PHP count and question check?
What ive got is a quiz style section, where a user logs in and attemps a quiz. The questions are obtained from a database table called kayA_theorytestqns and displayed randomly on the page. This bit works fine.
I then need to check the answer the user has checked against the database correctAnswer field
if the answer is correct i want it to add '1' to the score and then redirect back to the question page so another question is randomly generated. I also want to add '1' to the question number everytime the user answers a question, then when the total number of questions =35 show thier score!
the questions are on theorytest.php and the check page is on questioncheck.php!. I have used a form to submit the value of the chosen answer to the questioncheck.php page.
The code i have used in the questioncheck.php is shown below:-
<?php
$dbc = mysql_connect('*databasename and password*') or die ('Could not connect to MySQL :' .mysql_error());
mysql_select_db('*dbname*') or die('Could not connect to database :' .mysql_error());
session_start();
$answer = $_POST['answer'];
$random = $_SESSION['random'];
$score = '';
$_SESSION['score'] = $score;
$qnNumber = '';
$_SESSION['qnNumber'] = $qnNumber;
$query = "SELECT * FROM kayA_theorytestQns WHERE questionNumber = '$random'";
$result = mysql_query($query);
if($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if($answer == $row[correctAnswer])
{
$score+1;
$qnNumber+1;
if ($qnNumber > 34)
{
header('Location: theorytestEND.php');
}
else
{
header('Location: theorytest.php');
}
}
else
{
$score+0;
$qnNumber+1;
if ($qnNumber > 34)
{
header('Location: theorytestEND.php');
}
else
{
header('Location: theorytest.php');
}
}
}
else
{
echo "error in questioncheck.php";
}
mysql_close();
?>
cheers for any help again rich and nic
ASH
__________________
My new web design domain
www.askmultimedia.co.uk
|