Home > Backend Development > PHP Tutorial > How to use PHP to develop a simple online exam function

How to use PHP to develop a simple online exam function

PHPz
Release: 2023-09-21 09:20:01
Original
1741 people have browsed it

How to use PHP to develop a simple online exam function

How to use PHP to develop a simple online examination function, specific code examples are required

With the rapid development of the Internet, more and more schools and institutions have begun to adopt Assessment will be conducted through online examination. As a programming language widely used in web development, PHP can easily implement simple online examination functions. This article will introduce how to use PHP to develop a simple online examination system and provide specific code examples.

1. Database design
The online examination system needs to use a database to store examination questions and user answers. We can use MySQL database to achieve this. First, create a database named exam, and then create two tables: questions and users.

The questions table is used to store information about exam questions, including question ID (question_id), question content (question_content) and correct answer (question_answer). Here we take multiple-choice questions as an example, using only one option as the correct answer.

The SQL statement to create the table questions is as follows:

CREATE TABLE questions (
   question_id INT AUTO_INCREMENT PRIMARY KEY,
   question_content TEXT NOT NULL,
   question_answer CHAR(1) NOT NULL
);
Copy after login

The users table is used to store the user’s information and answer status, including user ID (user_id), user name (username) and the user’s answer status (answers). The answer status is stored in JSON format, where the key is the question ID and the value is the user's answer.

The SQL statement to create the users table is as follows:

CREATE TABLE users (
   user_id INT AUTO_INCREMENT PRIMARY KEY,
   username VARCHAR(50) NOT NULL,
   answers TEXT NOT NULL
);
Copy after login

2. Page design
The page design of the online examination system is relatively simple, including the login page and the examination page.

The login page (login.php) is used for users to log in to the system. After entering the user name, submit the form to enter the exam page.

<!DOCTYPE html>
<html>
<head>
   <title>在线考试系统 - 登录</title>
</head>
<body>
   <h2>登录</h2>
   <form action="exam.php" method="POST">
      <label for="username">用户名:</label>
      <input type="text" name="username" required>
      <br><br>
      <input type="submit" value="开始考试">
   </form>
</body>
</html>
Copy after login

The exam page (exam.php) is used to display exam questions and allow users to choose answers.

<!DOCTYPE html>
<html>
<head>
   <title>在线考试系统 - 考试</title>
</head>
<body>
   <h2>考试</h2>
   <form action="submit.php" method="POST">
      <?php
         // 连接数据库
         $conn = mysqli_connect("localhost", "root", "", "exam");
         if (!$conn) {
            die("数据库连接失败:" . mysqli_connect_error());
         }

         // 查询题目
         $sql = "SELECT * FROM questions";
         $result = mysqli_query($conn, $sql);

         // 显示题目
         if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
               echo "<h3>题目ID:" . $row['question_id'] . "</h3>";
               echo "<p>" . $row['question_content'] . "</p>";
               echo "<label><input type='radio' name='answers[" . $row['question_id'] . "]' value='A'> A</label>";
               echo "<label><input type='radio' name='answers[" . $row['question_id'] . "]' value='B'> B</label>";
               echo "<label><input type='radio' name='answers[" . $row['question_id'] . "]' value='C'> C</label>";
               echo "<label><input type='radio' name='answers[" . $row['question_id'] . "]' value='D'> D</label>";
               echo "<br><br>";
            }
         }

         // 关闭数据库连接
         mysqli_close($conn);
      ?>
      <input type="submit" value="提交答案">
   </form>
</body>
</html>
Copy after login

3. Answer submission and score statistics
After submitting the answer, the submitted answer will be processed and the total score will be calculated.

The submission page (submit.php) is used to process answers submitted by users, store user answers to the database, and calculate scores.

<?php
   // 连接数据库
   $conn = mysqli_connect("localhost", "root", "", "exam");
   if (!$conn) {
      die("数据库连接失败:" . mysqli_connect_error());
   }

   // 处理提交的答案
   $answers = $_POST['answers'];
   $username = $_COOKIE['username'];

   // 将答案存储到数据库
   $sql = "INSERT INTO users (username, answers) VALUES ('$username', '" . json_encode($answers) . "')";
   mysqli_query($conn, $sql);

   // 计算成绩
   $score = 0;
   $sql = "SELECT question_id, question_answer FROM questions";
   $result = mysqli_query($conn, $sql);
   while ($row = mysqli_fetch_assoc($result)) {
      if (isset($answers[$row['question_id']]) && $answers[$row['question_id']] == $row['question_answer']) {
         $score++;
      }
   }

   // 显示成绩
   echo "<h2>成绩:$score 分</h2>";

   // 关闭数据库连接
   mysqli_close($conn);
?>
Copy after login

The above is the method of using PHP to develop simple online examination functions, including database design, page design and implementation of answer submission and score statistics. This online exam system is just a simple example that can be expanded and optimized according to actual needs. Hope this article can be helpful to you!

The above is the detailed content of How to use PHP to develop a simple online exam function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template