I have been programming a web based quiz and I have stuck on this issue for a number of weeks now.
Basically I have used the var_dump function and I have found that infact my array is empty.
(C:\wamp64\www\MyProject\Quiz app\final.php:20: array (size=0) empty).
The issue is that my quiz can pull the questions and choices using mysql database, however it cannot validate inputs. So this is why I can't also use the score variable. How can I fix this issue so that my quiz can validate answers with my database ?.
Your help will very greatly appreciated and will help a beginner programmer like myself. Thank you very much indeed.
#Question.php
<?php session_start(); ?>
<?php include "database.php";
?>
<?php
//Set question
$number = (int) $_GET['n'];
/*
* Get Total Questions
*/
$query="SELECT * FROM questions" ;
//Get Results
$result = mysqli_query ($conn,$query);
$total = mysqli_num_rows($result);
/*
* Get the Question
*/
$query = "SELECT * FROM questions
WHERE question_number = $number";
//Get result
$result = mysqli_query ($conn,$query);
$question = mysqli_fetch_assoc($result);
/*
* Get choices
*/
$query2 = "SELECT * FROM choices
WHERE question_number = $number";
//Get result
$choices = mysqli_query($conn,$query2);
?>
<!DOCTYPE html PUBLIC'>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
General Knowledge Quiz
</title>
<link href="css/other.css" type="text/css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container" >
<div class="current">Question <?php echo $question ['question_number'];?> of <?php echo $total; ?> </div>
<p class="question">
<?php echo $question ['text']; ?>
</p>
<form action="process.php" method="post"
<ul class="choices"
<?php while ($row = $choices->fetch_assoc()) : ?>
<li><input name="choice" type="radio" value="<?php echo $row ['id'];?> " /><?php echo $row ['text']; ?></li>
<input type="submit" name="submit" value="Sumbit"/>
<input type="hidden" name= "number" value="<?php echo $number; ?>" />
</main>
</div>
</body>
</html>
<?php endwhile?>
#Process.php
<?php session_start(); ?>
<?php include "database.php";?>
<?php
// check to see if score is set error_handler)
if(!isset($_SESSION['score'] )) {
$_SESSION ['score'] = 0;
}
if($_POST) {
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number++;
echo $number ;
echo $selected_choice ;
}
/*
* Get Total Questions
*/
$query="SELECT * FROM questions" ;
//Get Results
$results = mysqli_query ($conn,$query);
$total = mysqli_num_rows($results);
/*
* Get correct choice
*/
$query = "SELECT * FROM choices
WHERE question_number = $number AND is_correct=1";
//Get result
$result = mysqli_query($conn,$query);
//Get Row
$row = $result->fetch_assoc();
//Set Correct choice
$correct_choice = $row['id'];
//Compare
if ($correct_choice == $selected_choice) {
//Answer is correct
$_SESSION['score']++;}
//Check if it is the last question
if($number == $total){
header("Location: final.php") ;
exit();
}
else {
header("Location: question.php?n=".$next);
}
```
Aucun commentaire:
Enregistrer un commentaire