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.

To create a multiple choice quiz in PHP and MySQL, you can follow these steps:

  • Create a MySQL database and table to store the quiz questions and answers.
  • Create a PHP script that connects to the MySQL database and retrieves the quiz questions and answers.
  • Use PHP to display the quiz questions and multiple choice answers in a web form.
  • When the user submits their answers, use PHP to check their answers against the correct answers in the MySQL database.
  • Display the user’s score and feedback on their performance.

Here’s an example of how you could implement a multiple choice quiz using PHP and MySQL:

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 –

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.

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.



PHP Multiple Choice quiz_ques and solutions



get_quiz_ques();

?>

Multiple Choice quiz_ques solutions

Please fill the details and solutions the all quiz_ques-

exam_choices($ques[0]); ?>

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

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.

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

Good Luck, You required to mark at least 50% to pass the exam.

'; } else { echo '

You have passed the exam and markd '.$mark.'%.

'; } ?>

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.

Leave a Comment