Home > Backend Development > PHP Tutorial > How to use PHP to implement a simple online class adjustment and leave request system

How to use PHP to implement a simple online class adjustment and leave request system

PHPz
Release: 2023-09-24 21:42:01
Original
900 people have browsed it

How to use PHP to implement a simple online class adjustment and leave request system

How to use PHP to implement a simple online class adjustment and leave request system

In modern education, students often need to adjust classes or ask for leave. In order to facilitate students and teachers to manage class adjustments and leave requests, we can use PHP to implement a simple online class adjustment and leave request system. This article will introduce in detail how to use PHP to implement this system and provide specific code examples.

  1. Design database structure

First, we need to design the database structure to store students' course information and leave records. The following is a simple database structure design:

表名: students
字段: id, name, email, password

表名: courses
字段: id, name, room, teacher

表名: schedule
字段: id, student_id, course_id, date, time

表名: leave_requests
字段: id, student_id, course_id, date_from, date_to, reason, status
Copy after login
  1. Create database connection

Next, we need to use PHP to connect to the database. Database connections can be implemented using extensions such as mysqli or PDO. The following is a sample code that uses mysqli extension to connect to the database:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "course_system";

// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}
?>
Copy after login
  1. Implementing student login and registration functions

Students need to log in to the system to adjust classes and request leave, so We need to implement student login and registration functions. The following is a simple sample code:

<?php
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
    if(isset($_POST['login'])) {
        $email = $_POST['email'];
        $password = $_POST['password'];

        // 查询数据库判断学生登录信息是否正确
        $sql = "SELECT id, name FROM students WHERE email = '$email' AND password = '$password'";
        $result = $conn->query($sql);

        if($result->num_rows == 1) {
            $row = $result->fetch_assoc();
            $_SESSION['id'] = $row['id'];
            $_SESSION['name'] = $row['name'];
            header("location: dashboard.php");
        } else {
            $error = "用户名或密码错误";
        }
    } elseif(isset($_POST['register'])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        // 插入学生注册信息到数据库
        $sql = "INSERT INTO students (name, email, password) VALUES ('$name', '$email', '$password')";
        if($conn->query($sql) === TRUE){
            $success = "注册成功,请登录";
        }else{
            $error = "注册失败";
        }
    }
}
?>

<form method="POST" action="">
    <h2>学生登录</h2>
    <input type="email" name="email" placeholder="请输入邮箱" required>
    <input type="password" name="password" placeholder="请输入密码" required>
    <button type="submit" name="login">登录</button>

    <h2>学生注册</h2>
    <input type="text" name="name" placeholder="请输入姓名" required>
    <input type="email" name="email" placeholder="请输入邮箱" required>
    <input type="password" name="password" placeholder="请输入密码" required>
    <button type="submit" name="register">注册</button>
</form>
Copy after login
  1. Implementing the function of adjusting classes and requesting leave

After students log in successfully, they can adjust classes and request leave. The following is a simple sample code:

<?php
session_start();

// 检查学生是否登录
if(!isset($_SESSION['id'])) {
    header("location: login.php");
    exit;
}

if($_SERVER["REQUEST_METHOD"] == "POST") {
    if(isset($_POST['change_course'])) {
        $course_id = $_POST['course_id'];
        $date = $_POST['date'];

        // 更新学生课程信息到数据库
        $sql = "UPDATE schedule SET course_id = '$course_id' WHERE student_id = ".$_SESSION['id']." AND date = '$date'";
        if($conn->query($sql) === TRUE){
            $success = "调课成功";
        }else{
            $error = "调课失败";
        }
    } elseif(isset($_POST['leave_request'])) {
        $course_id = $_POST['course_id'];
        $date_from = $_POST['date_from'];
        $date_to = $_POST['date_to'];
        $reason = $_POST['reason'];

        // 插入请假信息到数据库
        $sql = "INSERT INTO leave_requests (student_id, course_id, date_from, date_to, reason, status) VALUES (".$_SESSION['id'].", $course_id, '$date_from', '$date_to', '$reason', 'pending')";
        if($conn->query($sql) === TRUE){
            $success = "请假申请已提交";
        }else{
            $error = "请假申请失败";
        }
    }
}
?>

<form method="POST" action="">
    <h2>调课</h2>
    <select name="course_id">
        <?php
        // 查询学生课程信息
        $sql = "SELECT id, name FROM courses";
        $result = $conn->query($sql);
        while($row = $result->fetch_assoc()) {
            echo "<option value='".$row['id']."'>".$row['name']."</option>";
        }
        ?>
    </select>
    <input type="date" name="date" required>
    <button type="submit" name="change_course">确认调课</button>

    <h2>请假</h2>
    <select name="course_id">
        <?php
        // 查询学生课程信息
        $sql = "SELECT id, name FROM courses";
        $result = $conn->query($sql);
        while($row = $result->fetch_assoc()) {
            echo "<option value='".$row['id']."'>".$row['name']."</option>";
        }
        ?>
    </select>
    <input type="date" name="date_from" required>
    <input type="date" name="date_to" required>
    <textarea name="reason" placeholder="请输入请假原因" required></textarea>
    <button type="submit" name="leave_request">提交请假</button>
</form>
Copy after login

In the above code, we use Session to track whether students are logged in, and to process class transfer and leave requests. Simple page forms can be implemented using HTML.

With this simple sample code, we can implement a simple online class adjustment and leave request system. Of course, this is just a preliminary implementation and you can modify and extend it according to your specific needs. I hope this article will be helpful to implement a class adjustment and leave request system using PHP!

The above is the detailed content of How to use PHP to implement a simple online class adjustment and leave request system. 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