当前位置:首页 > PHP

php实现在线预约系统

2026-01-29 06:22:55PHP

数据库设计

创建一个MySQL数据库表存储预约信息,包含字段如id、name、email、phone、service_type、appointment_date、appointment_time、status等。示例SQL:

CREATE TABLE appointments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    service_type VARCHAR(50) NOT NULL,
    appointment_date DATE NOT NULL,
    appointment_time TIME NOT NULL,
    status ENUM('pending', 'confirmed', 'cancelled') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

表单界面

使用HTML和CSS创建预约表单,包含必要字段验证。示例表单代码:

<form action="submit_appointment.php" method="post">
    <input type="text" name="name" placeholder="Full Name" required>
    <input type="email" name="email" placeholder="Email" required>
    <input type="tel" name="phone" placeholder="Phone Number" required>
    <select name="service_type" required>
        <option value="">Select Service</option>
        <option value="consultation">Consultation</option>
        <option value="checkup">Checkup</option>
    </select>
    <input type="date" name="appointment_date" required>
    <input type="time" name="appointment_time" required>
    <button type="submit">Book Appointment</button>
</form>

后端处理

创建PHP脚本处理表单提交,验证数据并存入数据库:

php实现在线预约系统

<?php
// 连接数据库
$conn = new mysqli('localhost', 'username', 'password', 'appointment_db');

// 检查连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// 获取表单数据
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service_type = $_POST['service_type'];
$appointment_date = $_POST['appointment_date'];
$appointment_time = $_POST['appointment_time'];

// 插入数据
$sql = "INSERT INTO appointments (name, email, phone, service_type, appointment_date, appointment_time) 
        VALUES (?, ?, ?, ?, ?, ?)";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssss", $name, $email, $phone, $service_type, $appointment_date, $appointment_time);

if ($stmt->execute()) {
    echo "Appointment booked successfully!";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$stmt->close();
$conn->close();
?>

预约管理后台

创建管理员界面查看和管理预约:

// 查询所有预约
$sql = "SELECT * FROM appointments ORDER BY appointment_date, appointment_time";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<tr>
                <td>".$row["name"]."</td>
                <td>".$row["email"]."</td>
                <td>".$row["appointment_date"]."</td>
                <td>".$row["appointment_time"]."</td>
                <td>".$row["status"]."</td>
                <td>
                    <a href='confirm.php?id=".$row["id"]."'>Confirm</a>
                    <a href='cancel.php?id=".$row["id"]."'>Cancel</a>
                </td>
              </tr>";
    }
}

时间冲突检查

在提交预约前检查时间是否已被占用:

php实现在线预约系统

$check_sql = "SELECT id FROM appointments 
              WHERE appointment_date = ? 
              AND appointment_time = ?";
$check_stmt = $conn->prepare($check_sql);
$check_stmt->bind_param("ss", $appointment_date, $appointment_time);
$check_stmt->execute();
$check_result = $check_stmt->get_result();

if ($check_result->num_rows > 0) {
    echo "This time slot is already booked. Please choose another time.";
    exit;
}

邮件通知

使用PHP邮件函数发送确认邮件:

$to = $email;
$subject = "Appointment Confirmation";
$message = "Dear $name,\n\nYour appointment for $service_type on $appointment_date at $appointment_time has been booked.";
$headers = "From: appointments@yourdomain.com";

mail($to, $subject, $message, $headers);

安全防护

添加基本安全措施防止SQL注入和XSS攻击:

// 过滤输入
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);

// 使用预处理语句防止SQL注入
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssss", $name, $email, $phone, $service_type, $appointment_date, $appointment_time);

完整系统扩展

考虑添加以下功能增强系统:

  • 用户注册登录系统
  • 预约修改功能
  • 预约提醒功能
  • 日历视图展示可用时间段
  • 多语言支持
  • 支付集成(如需收费服务)

标签: 在线系统
分享给朋友:

相关文章

Vue在线表格实现

Vue在线表格实现

Vue在线表格实现方案 使用Element UI的el-table组件 Element UI提供了功能强大的表格组件el-table,支持排序、筛选、分页等常见功能。安装Element UI后可直接使…

使用vue前端实现的系统vue

使用vue前端实现的系统vue

Vue 前端系统实现的核心步骤 项目初始化与配置 通过 Vue CLI 或 Vite 创建项目,安装必要依赖(如 Vue Router、Pinia/Vuex、Axios)。配置基础目录结构,区分组件、…

vue实现点餐系统

vue实现点餐系统

实现点餐系统的基本思路 使用Vue.js实现点餐系统需要结合前端框架特性,合理设计组件结构和数据流。以下为关键实现步骤: 核心组件结构设计 MenuList组件:展示菜品列表,包含图片、名称、价格和…

实现react事件系统

实现react事件系统

React 事件系统实现原理 React 的事件系统是合成事件(SyntheticEvent)系统,它是对原生 DOM 事件的跨浏览器包装器。React 通过事件委托机制将所有事件绑定到 docume…

php实现在线考试

php实现在线考试

PHP实现在线考试系统 数据库设计 在线考试系统需要设计合理的数据库结构。常见表包括用户表、试题表、考试记录表等。 用户表(users)示例结构: CREATE TABLE users (…

js实现ppt在线预览

js实现ppt在线预览

实现PPT在线预览的JavaScript方案 在浏览器中实现PPT文件的在线预览,可以通过多种技术方案实现。以下是几种常见的方法: 使用Office Online Viewer嵌入 微软提供了免费的…