php实现在线预约系统
数据库设计
创建数据库表存储预约信息,通常包括用户信息、预约时间、服务类型等字段。示例SQL语句:
CREATE TABLE appointments (
id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(100) NOT NULL,
user_email VARCHAR(100) 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表单收集用户预约信息,包含必要验证:
<form action="process_booking.php" method="post">
<input type="text" name="name" placeholder="姓名" required>
<input type="email" name="email" placeholder="邮箱" required>
<select name="service" required>
<option value="">选择服务类型</option>
<option value="consultation">咨询</option>
<option value="checkup">检查</option>
</select>
<input type="date" name="date" min="<?= date('Y-m-d') ?>" required>
<input type="time" name="time" required>
<button type="submit">提交预约</button>
</form>
处理预约逻辑
创建PHP脚本处理表单提交,验证数据并存入数据库:

<?php
$conn = new mysqli('localhost', 'username', 'password', 'booking_system');
$name = $_POST['name'];
$email = $_POST['email'];
$service = $_POST['service'];
$date = $_POST['date'];
$time = $_POST['time'];
$stmt = $conn->prepare("INSERT INTO appointments (user_name, user_email, service_type, appointment_date, appointment_time) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $name, $email, $service, $date, $time);
$stmt->execute();
header("Location: confirmation.php");
?>
预约冲突检查
在插入数据库前检查时间是否已被占用:
$check = $conn->prepare("SELECT id FROM appointments WHERE appointment_date = ? AND appointment_time = ?");
$check->bind_param("ss", $date, $time);
$check->execute();
if ($check->get_result()->num_rows > 0) {
die("该时间段已被预约");
}
管理员界面
创建后台管理界面查看和管理预约:

$result = $conn->query("SELECT * FROM appointments ORDER BY appointment_date, appointment_time");
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['user_name']}</td>
<td>{$row['appointment_date']} {$row['appointment_time']}</td>
<td>{$row['service_type']}</td>
<td>{$row['status']}</td>
</tr>";
}
邮件通知
使用PHPMailer发送预约确认邮件:
require 'PHPMailer/PHPMailer.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->setFrom('noreply@example.com');
$mail->addAddress($email);
$mail->Subject = '预约确认';
$mail->Body = "尊敬的{$name},您的{$service}预约已确认,时间:{$date} {$time}";
$mail->send();
安全防护
添加基础安全措施防止SQL注入和XSS攻击:
function sanitize($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
系统可根据需求扩展功能如:预约提醒、用户登录系统、支付集成、多语言支持等。






