Building a Simple Online Quiz System with PHP and MySQL
This tutorial explains how to create a basic online quiz platform by designing a MySQL database, writing PHP code to connect, retrieve, display questions, and process user answers to calculate scores, providing a foundation for further feature expansion.
With the rise of online learning, many developers need a convenient way to deliver quizzes that students can take anytime; this article shows how to build such a system using PHP and MySQL.
Data storage and management : Create a MySQL database named test_questions and a table questions with columns id, question, options, and answer to hold each quiz item.
CREATE TABLE questions (
id int(11) NOT NULL AUTO_INCREMENT,
question varchar(255) NOT NULL,
options text NOT NULL,
answer varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;Connecting to the database in PHP :
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test_questions";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>Fetching quiz data with a helper function:
<?php
function getQuestions() {
global $conn;
$sql = "SELECT * FROM questions";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$questions = [];
while ($row = $result->fetch_assoc()) {
$question = [
'id' => $row['id'],
'question' => $row['question'],
'options' => json_decode($row['options'], true),
'answer' => $row['answer'],
];
$questions[] = $question;
}
return $questions;
} else {
return [];
}
}
?>Displaying the questions on a web page using a loop:
<?php
$questions = getQuestions();
?>
<?php foreach ($questions as $question): ?>
<h3><?php echo $question['question']; ?></h3>
<?php foreach ($question['options'] as $option): ?>
<label>
<input type="radio" name="answer_<?php echo $question['id']; ?>" value="<?php echo $option; ?>">
<?php echo $option; ?>
</label>
<?php endforeach; ?>
<?php endforeach; ?>Submitting answers and calculating the score after the user posts the form:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$answers = $_POST;
$score = 0;
foreach ($questions as $question) {
$userAnswer = $answers["answer_{$question['id']}"];
if ($userAnswer === $question['answer']) {
$score++;
}
}
echo "Total score: " . $score;
}
?>The example demonstrates the core steps—database design, data retrieval, rendering, and scoring—required to implement a functional online quiz. Developers can extend it with additional question types, analytics, or user authentication as needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
