php mysql multiple choice quiz source code Example – How to create a multiple choice quiz in PHP and MySQL?

php mysql multiple choice quiz source code : In this Turorial, you will learn to make a php mysql multiple choice quiz source code MySql with PHP Basic MCQ.

php mysql multiple choice quiz source code

how to create quiz in php and mysql?, online question has a lot of big advantages. It is considered an easy best solution to conduct a question, mcq questions gk, engage your concertgoer, mcq questions with answers for engineering, more and lots of participants as well as randomizing questions.

Also beneficial for security and confidentiality of questions as well as solutions and no instructor needed. It also saves paper, users time, as well as money and it’s more secure.

Step By Step “php mysql multiple choice quiz source code”.

Creating Database

A database required to be created to store the information about questions, options and correct solutions. Therefor let’s make a database using the following query. Here is a table ‘questions’ that contains all the quiz questions.

CREATE TABLE IF NOT EXISTS `questions` (
  `ques_id` int(11) NOT NULL AUTO_INCREMENT,
  `question` varchar(150) NOT NULL,
  `is_enabled` int(11) NOT NULL,
  PRIMARY KEY (`ques_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
INSERT INTO `questions` (`ques_id`, `question`, `is_enabled`) VALUES
(1, 'Why is processing a sorted array faster than processing an unsorted array?', 1),
(2, 'How do I undo the most recent local commits in Git?', 1),
(3, 'How do I delete a Git branch locally and remotely?', 1),
(4, 'What is the difference between 'git pull' and 'git fetch'?', 1);

Here is a table ‘question_options’ that contains all the question options –

CREATE TABLE IF NOT EXISTS `question_options` (
  `option_id` int(11) NOT NULL AUTO_INCREMENT,
  `ques_id` int(11) NOT NULL,
  `option` varchar(150) NOT NULL,
  `is_enabled` int(11) NOT NULL,
  PRIMARY KEY (`option_id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
INSERT INTO `question_options` (`option_id`, `ques_id`, `option`, `is_enabled`) VALUES
(1, 1, 'optimizilla', 1),
(2, 1, 'javplayer', 1),
(3, 1, 'locktype', 1),
(4, 1, 'check_circle', 1),
(5, 2, 'imagesrc', 1),
(6, 2, 'captcha', 1),
(7, 2, 'EnlighterJSWrapper()', 1),
(8, 2, 'leftSideArrow()', 1),
(9, 3, 'animation()', 1),
(10, 3, 'repository()', 1),
(11, 3, 'explicit()', 1),
(12, 3, 'exist()', 1),
(13, 4, 'end()', 1),
(14, 4, 'gud()', 1),
(15, 4, 'master()', 1),
(16, 4, 'remote()', 1);

At last, i make a table ‘question_solution’ for storing the correct solution of the question –

CREATE TABLE IF NOT EXISTS `question_solution` (
  `quiz_id` int(11) NOT NULL AUTO_INCREMENT,
  `ques_id` int(11) NOT NULL,
  `option_number` int(11) NOT NULL,
  PRIMARY KEY (`quiz_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
INSERT INTO `question_solution` (`quiz_id`, `ques_id`, `option_number`) VALUES
(1, 1, 2),
(2, 2, 1),
(3, 3, 3),
(4, 4, 4);

Source Code

These are the PHP full source code for multiple choice questions and solutions. For step by step, i have used the MySQLi object-oriented programming to optimize the database connection source code.

questionclass.php
First of all, I have taken variables for database details, please here your working replace them with your secure information. Next, i have taken a constructor function for database connectivity code.

db)){
	// simple 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' ";
 $row_data = mysqli_query($this->db ,$select);
 return mysqli_fetch_all($row_data);
}
public function question_options($ques_id) {
 $select = "SELECT * FROM `question_options` where ques_id = '$ques_id' AND is_enabled = '1'  ";
 $row_data = mysqli_query($this->db ,$select);
 return mysqli_fetch_all($row_data);
} 
public function solution($ques_id) {
 $select = "SELECT * FROM `question_solution` where ques_id = '$ques_id' ";
 $row_data = mysqli_query($this->db ,$select);
 return mysqli_fetch_all($row_data);
} 
}
?>

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

index.php
This is the main index.php file that i will call in the browser like as a Chrome. In this, i have imported the ‘questionclass.php’ and loop over the questions as well as all the available options data. When the member submits the form, it will redirect to the ‘score.php’ page.



PHP Multiple Choice Questions and Answers - www.pakainfo.com



get_questions();

?>

Multiple Choice Questions Answers

Please fill the details and solutions the all questions-

question_options($ques[0]); ?>

    ".$option[3].""; } ?>

score.php
In this web page, i collect the post data as well as basic calculate the score of the member. For this, i have imported the ‘questionclass.php’ database class file as well as compared the correct solution with the member’s.

$v)
{
	$solution = $db->solution($k);
	if($solution[0][2] == $v) { // option is correct
		$score++;
	}
}
$score = $score	/ 4 *100;
if($score < 50)
{
	echo '

You required to score at least 50% to pass the exam.

'; } else { echo '

You have passed the exam and scored '.$score.'%.

'; } ?>

I hope you get an idea about php mysql multiple choice quiz source code.
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.

Leave a Comment