php实现在线预约系统
数据库设计
创建数据库表存储预约信息,通常包括用户ID、预约时间、服务类型、状态等字段。使用MySQL为例:
CREATE TABLE appointments (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
service_type VARCHAR(50) NOT NULL,
appointment_time DATETIME NOT NULL,
status ENUM('pending', 'confirmed', 'cancelled') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
预约表单实现
构建HTML表单收集用户预约信息,包含服务类型选择和时间选择器:
<form action="book_appointment.php" method="post">
<select name="service_type" required>
<option value="consultation">咨询</option>
<option value="checkup">检查</option>
</select>
<input type="datetime-local" name="appointment_time" required>
<button type="submit">提交预约</button>
</form>
处理预约逻辑
创建PHP脚本处理表单提交,验证时间冲突并存入数据库:
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$userId = $_SESSION['user_id'];
$serviceType = $conn->real_escape_string($_POST['service_type']);
$appointmentTime = $conn->real_escape_string($_POST['appointment_time']);
// 检查时间是否可用
$check = $conn->query("SELECT id FROM appointments
WHERE appointment_time = '$appointmentTime'");
if ($check->num_rows === 0) {
$conn->query("INSERT INTO appointments
(user_id, service_type, appointment_time)
VALUES ($userId, '$serviceType', '$appointmentTime')");
echo "预约成功";
} else {
echo "该时间段已被预约";
}
}
?>
预约管理界面
创建管理员界面显示所有预约,支持状态修改:
$appointments = $conn->query("SELECT * FROM appointments ORDER BY appointment_time");
while ($row = $appointments->fetch_assoc()) {
echo "ID: {$row['id']} 时间: {$row['appointment_time']}
状态: {$row['status']}
<a href='update_status.php?id={$row['id']}&status=confirmed'>确认</a>
<a href='update_status.php?id={$row['id']}&status=cancelled'>取消</a><br>";
}
邮件通知功能
集成PHPMailer发送预约确认邮件:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('noreply@example.com');
$mail->addAddress($userEmail);
$mail->Subject = '预约确认';
$mail->Body = "您的预约已确认,时间:$appointmentTime";
$mail->send();
时间冲突检测
添加JavaScript前端验证防止选择过去时间:
document.querySelector('input[name="appointment_time"]').min =
new Date().toISOString().slice(0, 16);
安全性增强
对输入数据进行过滤和预处理,防止SQL注入:
$stmt = $conn->prepare("INSERT INTO appointments
(user_id, service_type, appointment_time)
VALUES (?, ?, ?)");
$stmt->bind_param("iss", $userId, $serviceType, $appointmentTime);
$stmt->execute();






