how to create quiz in php and mysql? (4 Questions)

how to create quiz in php and mysql? : You can download Multiple Choice Quiz System for your php major or mini source code and database.

How to create a multiple choice quiz in PHP and MySQL?

In this post php quiz code, you will learn to how to create quiz in PHP and MySQL. Today, online quiz has a lot of advantages. It is considered an easy solution to conduct quiz, Online Examination System Project, engage your more helpful audience, large Big number of participants as well as every time randomizing more Questions & Answers.

how to create quiz in php and mysql
how to create quiz in php and mysql

In php mysql multiple choice quiz source code,Also advantage for security and professionals of quiz Questions & Answers and solutions and no instructor any types of the required. It also saves paper work, no wasting time, no any charge or money and it’s more great way and secure.

Creating Database : php quiz code with database

A database need to be created to store the information about Questions & Answers, choices and correct solutions. So let’s create a database using the following query. Here is a table ‘quiz_ques’ that contain all the quiz Questions & Answers.

quiz_tables
quiz_tables

CREATE TABLE IF NOT EXISTS `quiz_ques` (
  `exam_qid` int(11) NOT NULL AUTO_INCREMENT,
  `question` varchar(150) NOT NULL,
  `is_active` int(11) NOT NULL,
  PRIMARY KEY (`exam_qid`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

INSERT INTO `quiz_ques` (`exam_qid`, `question`, `is_active`) VALUES
(1, 'Which function is used to reverse the order of elements in an array?', 1),
(2, 'Which function is used to return character from the ASCII value?', 1),
(3, 'Which function is used to check the existence of a constant?', 1),
(4, 'Which function is used to return the last element of an array?', 1);

Here is a table ‘exam_choices’ that contains all the exam choices –

CREATE TABLE IF NOT EXISTS `exam_choices` (
  `choice_id` int(11) NOT NULL AUTO_INCREMENT,
  `exam_qid` int(11) NOT NULL,
  `choice` varchar(150) NOT NULL,
  `is_active` int(11) NOT NULL,
  PRIMARY KEY (`choice_id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;

INSERT INTO `exam_choices` (`choice_id`, `exam_qid`, `choice`, `is_active`) VALUES
(1, 1, 'array_rev()', 1),
(2, 1, 'array_reverse()', 1),
(3, 1, 'reverse()', 1),
(4, 1, 'array_end()', 1),
(5, 2, 'chr()', 1),
(6, 2, 'ascii()', 1),
(7, 2, 'asc()', 1),
(8, 2, 'return_chr()', 1),
(9, 3, 'define()', 1),
(10, 3, 'const()', 1),
(11, 3, 'defined()', 1),
(12, 3, 'exist()', 1),
(13, 4, 'end()', 1),
(14, 4, 'arr_end()', 1),
(15, 4, 'last()', 1),
(16, 4, 'end()', 1);

At last, I make a table ‘exam_solution’ for storing the correct solution of the exam –
CREATE TABLE IF NOT EXISTS `exam_solution` (
  `qa_id` int(11) NOT NULL AUTO_INCREMENT,
  `exam_qid` int(11) NOT NULL,
  `choice_number` int(11) NOT NULL,
  PRIMARY KEY (`qa_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

INSERT INTO `exam_solution` (`qa_id`, `exam_qid`, `choice_number`) VALUES
(1, 1, 2),
(2, 2, 1),
(3, 3, 3),
(4, 4, 4);

php mysql multiple choice quiz source code
php mysql multiple choice quiz source code

Source Code for how to create quiz in php and mysql?

These are the PHP source code for multiple choice quiz_ques and solutions. For simplicity, we have used the MySQLi object oriented programming to simplify the database connection code –

Read Also:  sum of two textbox values into third textbox using javascript

examclass.php

First, we have taken variables for database credentials, please replace with your credentials. Next, we have taken constructor function for database connectivity code.

<?php

class Exam {
// Database credentials
private $host     = 'localhost';
private $username = 'root';
private $password = '[email protected]';
private $database = 'exam_centers';
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_quiz_ques(){
 $select = "SELECT * FROM `quiz_ques` where is_active = '1' ";
 $result = mysqli_query($this->db ,$select);
 return mysqli_fetch_all($result);
}
public function exam_choices($exam_qid) {
 $select = "SELECT * FROM `exam_choices` where exam_qid = '$exam_qid' AND is_active = '1'  ";
 $result = mysqli_query($this->db ,$select);
 return mysqli_fetch_all($result);
} 
public function solution($exam_qid) {
 $select = "SELECT * FROM `exam_solution` where exam_qid = '$exam_qid' ";
 $result = mysqli_query($this->db ,$select);
 return mysqli_fetch_all($result);
} 
}
?>

index.php

This is the main PHP root file that I will call on the webpage browser. In this, I have imported the ‘examclass.php’ and loop over the quiz_ques and choices data. When the student submits the form, it will redirect to the ‘mark.php’ page.

Read Also:  Jquery File Upload Size validation Example

<html>
<head>
<title>PHP Multiple Choice quiz_ques and solutions</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<?php
	include 'examclass.php';
	$db = new Exam();
	$quesions = $db->get_quiz_ques();

?>
<div class="container">
<h1>Multiple Choice quiz_ques solutions</h1>
<p>Please fill the details and solutions the all quiz_ques-</p>
<div class="form-group">
<form action="mark.php" method="post">
<?php
foreach($quesions as $ques) {
$choices = $db->exam_choices($ques[0]);

?>
<h4><?php echo $ques[1]; ?></h4>
<div class="input-group-text" style="text-align: left; font-size: 18px;"> 
<ol>
<?php
foreach($choices as $choice) { 
 echo "<li><input type='radio' name='".$choice[2]."' value='".$choice[1]."' required/> ".$choice[3]."</li>";
}
?>
</ol>
</div>

</div>
<div class="form-group">
<input type="submit" value="Submit" name="submit" class="btn btn-primary"/>
</div>
</form>
</div>
</body>
</html>

mark.php

In this page, I All the useful data get the post data and calculate the mark of the student. For this, I have imported the ‘examclass.php’ database class file as well as compared the correct solution with student.

Read Also:  Laravel 6 User Roles and Permissions Tutorial

<?php 
include 'examclass.php';
$db = new Exam();
$mark = 0;
foreach($_POST as $k=>$v)
{
	$solution = $db->solution($k);
	if($solution[0][2] == $v) { // choice is correct
		$mark++;
	}
}
$mark = $mark	/ 4 *100;
if($mark < 50)
{
	echo '<h2>Good Luck, You required to mark at least 50% to pass the exam.</h2>';
}
else {
	echo '<h2>You have passed the exam and markd '.$mark.'%.</h2>';
}
?>

I hope you get an idea about “how to create quiz in php and mysql”.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.