I used your database structure and values. You have an issue with the data in quiz_answer. I am going to leave it to you to figure out what is wrong
(Hint: the answers need to match the correct option_id!)
Here are the files I used. You will need to have valid database host, name, password and db variables defined in the quiz class, so that you are connecting to the database.
One thing you need to understand is that when you submit a form ALL the fields in the form are submitted, including buttons. Everything will be in the $_POST array.
I added a line of code to deal with this problem.
I also added some code to display the grading step for each question, so you can better understand what is happening.
Some good functions you can use to help you with simple debugging:
When you don't understand what a variable contains try adding print_r($variable);die();
This will show you the value of the variable and stop processing. You don't have to stop processing, but having a lot of these statements is often confusing. Solve one issue and move on to the next.
The biggest issue with this code is the use of numeric indexing of your mysql data with mysqli_fetch_all. Things would be better if your mysqli fetch routines used mysqli_fetch_all($result, MYSQLI_ASSOC);
Working with array keys is much clearer, not to mention safer than having code like: (if $somevar == $array[0][2]). That code would be something like $somevar == $array[0]['option_number'] instead.
//quizclass.php
<?php
class Quiz {
// Database credentials
private $host = 'hostname';
private $username = 'username';
private $password = 'password';
private $database = 'dbname';
public $db;
public function __construct()
{
if (!isset($this->db)) {
// Connect to the database
try {
$this->db = new mysqli($this->host, $this->username, $this->password, $this->database);
} catch (Exception $e) {
$error = $e->getMessage();
echo $error;
}
}
}
public function get_questions()
{
$select = "SELECT * FROM `questions` where is_enabled = '1' ";
$result = mysqli_query($this->db, $select);
return mysqli_fetch_all($result);
}
public function quiz_options($qid)
{
$select = "SELECT * FROM `quiz_options` where qid = '$qid' AND is_enabled = '1' ";
$result = mysqli_query($this->db, $select);
return mysqli_fetch_all($result);
}
public function answer($qid)
{
$select = "SELECT * FROM `quiz_answer` where qid = '$qid' ";
$result = mysqli_query($this->db, $select);
return mysqli_fetch_all($result);
}
}
//index.php
<html>
<head>
<title>PHP Multiple Choice Questions and Answers</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<?php
include 'quizclass.php';
$db = new Quiz();
$quesions = $db->get_questions();
?>
<div class="container">
<h1>Multiple Choice Questions Answers</h1>
<p>Please fill the details and answers the all questions-</p>
<form action="score.php" method="post">
<?php
foreach($quesions as $ques) {
$options = $db->quiz_options($ques[0]);
?>
<div class="form-group">
<h4><?php echo $ques[1]; ?></h4>
<div class="input-group-text" style="text-align: left; font-size: 18px;">
<ol>
<?php foreach($options as $option) {
echo "<li><input type='radio' name='".$option[1]."' value='".$option[0]."'> ".$option[2]."</li>";
}?>
</ol>
</div>
<?php
} //end foreach
?>
<div class="form-group text-center">
<input type="submit" value="Submit" name="submit" class="btn btn-primary"/>
</div>
</form>
</div>
</body>
</html>
//score.php
<?php
include 'quizclass.php';
$db = new Quiz();
$score = 0;
foreach ($_POST as $k => $v) {
if (is_int($k)) {
$answer = $db->answer($k);
if ($answer[0][2] == $v) {
// option is correct
echo "Question {$answer[0][1]} was correct.<br>";
$score++;
} else {
echo "Question {$answer[0][1]} was wrong.<br>";
}
}
}
$score = $score / 4 *100;
if($score < 50) {
echo '<h2>You need to score at least 50% to pass the exam.</h2>';
} else {
echo '<h2>You have passed the exam and scored '.$score.'%.</h2>';
}