Home  >  Article  >  Backend Development  >  How to use PHP to implement a simple online exam and score query system

How to use PHP to implement a simple online exam and score query system

WBOY
WBOYOriginal
2023-09-25 08:28:431125browse

How to use PHP to implement a simple online exam and score query system

How to use PHP to implement a simple online examination and score query system

随着网络的发展,在线考试和成绩查询系统在教育和培训领域得到了广泛应用。利用PHP编程语言,我们可以快速实现一个简单的在线考试和成绩查询系统,为学生提供方便的考试和查询服务。下面将介绍如何使用PHP实现该系统,并提供具体的代码示例。

1. System function design

The basic functions of the examination system include students There are three parts: registration, examination, and score inquiry. Students need to provide basic information when registering, including student number, name, class, etc. The exam section provides two types of questions: multiple-choice questions and fill-in-the-blank questions. Students can choose answers or fill in the answers according to the questions. In the score inquiry section, students’ test scores can be queried based on their student number or name.

2. System architecture design

In order to realize the examination and score query functions, we need to design the database and corresponding table structure. Create a database named exam in the MySQL database, which contains two tables: students and scores. The students table is used to store basic information of students, and the scores table is used to store students' test scores.

The structure of the students table is as follows:

CREATE TABLE students (
    id INT(11) AUTO_INCREMENT PRIMARY KEY,
    studentID VARCHAR(20) NOT NULL,
    name VARCHAR(50) NOT NULL,
    class VARCHAR(50) NOT NULL
);

The structure of the scores table is as follows:

CREATE TABLE scores (
    id INT(11) AUTO_INCREMENT PRIMARY KEY,
    studentID VARCHAR(20) NOT NULL,
    score INT(11) NOT NULL
);

3. System code implementation

  1. Student registration part

In the register.php file, write the following code to implement the student registration function:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $studentID = $_POST["studentID"];
    $name = $_POST["name"];
    $class = $_POST["class"];

    $conn = new mysqli("localhost", "username", "password", "exam"); // 修改为实际的数据库连接信息

    $sql = "INSERT INTO students (studentID, name, class) VALUES ('$studentID', '$name', '$class')";
    if ($conn->query($sql) === TRUE) {
        echo "注册成功!";
    } else {
        echo "注册失败!";
    }

    $conn->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>学生注册</title>
</head>
<body>
    <h2>学生注册</h2>
    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        学号:<input type="text" name="studentID"><br>
        姓名:<input type="text" name="name"><br>
        班级:<input type="text" name="class"><br>
        <input type="submit" value="注册">
    </form>
</body>
</html>
  1. Exam part

In the exam.php file , write the following code to implement the exam function:

<!DOCTYPE html>
<html>
<head>
    <title>考试</title>
</head>
<body>
    <h2>考试</h2>
    <form method="POST" action="submit.php">
        <h3>选择题</h3>
        1. PHP是一种什么类型的编程语言?<br>
        A. 面向对象编程语言<br>
        B. 脚本编程语言<br>
        C. 编译型语言<br>
        D. 结构化编程语言<br>
        <input type="radio" name="answer1" value="A"> A
        <input type="radio" name="answer1" value="B"> B
        <input type="radio" name="answer1" value="C"> C
        <input type="radio" name="answer1" value="D"> D<br>

        <h3>填空题</h3>
        2. PHP的全称是__?__ Hypertext Preprocessor.<br>
        <input type="text" name="answer2"><br>

        <input type="submit" value="提交">
    </form>
</body>
</html>
  1. Submit answer part

In the submit.php file, write the following code to process the answers submitted by students and calculate the scores:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $answer1 = $_POST["answer1"];
    $answer2 = $_POST["answer2"];

    $score = 0;
    if ($answer1 == "B") {
        $score += 50;
    }
    if ($answer2 == "PHP") {
        $score += 50;
    }

    session_start();
    $studentID = $_SESSION["studentID"];

    $conn = new mysqli("localhost", "username", "password", "exam"); // 修改为实际的数据库连接信息

    $sql = "INSERT INTO scores (studentID, score) VALUES ('$studentID', '$score')";
    if ($conn->query($sql) === TRUE) {
        echo "提交成功!成绩为:" . $score;
    } else {
        echo "提交失败!";
    }

    $conn->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>提交</title>
</head>
<body>
</body>
</html>
  1. Grade query part

In the query.php file, write the following code to implement the student grade query function:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $studentID = $_POST["studentID"];

    $conn = new mysqli("localhost", "username", "password", "exam"); // 修改为实际的数据库连接信息

    $sql = "SELECT * FROM scores WHERE studentID = '$studentID'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            echo "学号:" . $row["studentID"] . ",成绩:" . $row["score"] . "<br>";
        }
    } else {
        echo "未查询到成绩信息!";
    }

    $conn->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>成绩查询</title>
</head>
<body>
    <h2>成绩查询</h2>
    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        学号:<input type="text" name="studentID"><br>
        <input type="submit" value="查询">
    </form>
</body>
</html>

4. System operation test

Save the above code as four files: register.php, exam.php, submit.php and query.php, and deploy them to a web server that supports PHP. Visit register.php in the browser to register students, visit exam.php to take the exam, and visit query.php to query the results.

Through the above examples, we implemented a simple online exam and score query system using PHP. I believe that through learning and practice, you can further enrich and improve the system to meet more needs.

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

Statement:
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