📅 ระบบจองเวลานัดหมาย (PHP + MySQL + LINE Notify)
✅ ฟีเจอร์หลัก
- 📌 เลือกวันและเวลานัดหมาย ด้วย Datepicker
- 📌 บันทึกข้อมูลการนัดหมาย ลง MySQL
- 📌 แจ้งเตือนผ่าน LINE Notify เมื่อลูกค้าทำการนัดหมาย
- 📌 รองรับมือถือ (Responsive Design)
- 📌 ผู้ดูแลระบบสามารถดูและจัดการการนัดหมายได้
📌 1. สร้างฐานข้อมูล MySQL
CREATE DATABASE appointment_system;
USE appointment_system;
-- ตารางการนัดหมาย
CREATE TABLE appointments (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
phone VARCHAR(20) NOT NULL,
email VARCHAR(255) NOT NULL,
appointment_date DATETIME NOT NULL,
message TEXT,
status ENUM('pending', 'confirmed', 'cancelled') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
📌 2. เชื่อมต่อฐานข้อมูล (db.php
)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "appointment_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("เชื่อมต่อฐานข้อมูลล้มเหลว: " . $conn->connect_error);
}
?>
📌 3. ฟอร์มจองนัดหมาย (appointment_form.php
)
<?php include 'db.php'; ?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>จองเวลานัดหมาย</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css">
</head>
<body>
<div class="container mt-5">
<h2>จองเวลานัดหมาย</h2>
<form method="post" action="submit_appointment.php">
ชื่อ: <input type="text" name="name" class="form-control" required>
เบอร์โทร: <input type="text" name="phone" class="form-control" required>
อีเมล: <input type="email" name="email" class="form-control" required>
วันที่และเวลา: <input type="text" id="appointment_date" name="appointment_date" class="form-control" required>
ข้อความเพิ่มเติม: <textarea name="message" class="form-control"></textarea>
<button type="submit" class="btn btn-primary mt-3">จองนัดหมาย</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
$("#appointment_date").datepicker({ dateFormat: "yy-mm-dd", minDate: 0 });
});
</script>
</body>
</html>
📌 4. บันทึกการนัดหมายและแจ้งเตือน LINE (submit_appointment.php
)
<?php
include 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$appointment_date = $_POST['appointment_date'];
$message = $_POST['message'];
$stmt = $conn->prepare("INSERT INTO appointments (name, phone, email, appointment_date, message) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $name, $phone, $email, $appointment_date, $message);
if ($stmt->execute()) {
sendLineNotification($name, $appointment_date, $phone);
echo "การจองสำเร็จ!";
} else {
echo "เกิดข้อผิดพลาด: " . $stmt->error;
}
}
// ฟังก์ชันแจ้งเตือนผ่าน LINE
function sendLineNotification($name, $appointment_date, $phone) {
$line_token = "YOUR_LINE_NOTIFY_TOKEN"; // เปลี่ยนเป็น Token ของคุณ
$message = "🔔 มีการจองใหม่จาก: $name\n📅 วันที่: $appointment_date\n📞 เบอร์โทร: $phone";
$data = http_build_query(['message' => $message]);
$options = [
"http" => [
"header" => "Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Bearer $line_token\r\n",
"method" => "POST",
"content" => $data
]
];
$context = stream_context_create($options);
file_get_contents("https://notify-api.line.me/api/notify", false, $context);
}
?>
<a href="appointment_form.php">กลับไปที่ฟอร์ม</a>
📌 5. หน้าผู้ดูแลระบบดูการนัดหมาย (admin_appointments.php
)
<?php
include 'db.php';
$result = $conn->query("SELECT * FROM appointments ORDER BY appointment_date ASC");
?>
<h2>รายการนัดหมาย</h2>
<table border="1">
<tr>
<th>ชื่อ</th>
<th>เบอร์โทร</th>
<th>อีเมล</th>
<th>วันเวลา</th>
<th>ข้อความ</th>
<th>สถานะ</th>
<th>จัดการ</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['name'] ?></td>
<td><?= $row['phone'] ?></td>
<td><?= $row['email'] ?></td>
<td><?= $row['appointment_date'] ?></td>
<td><?= $row['message'] ?></td>
<td><?= ucfirst($row['status']) ?></td>
<td>
<a href="update_status.php?id=<?= $row['id'] ?>&status=confirmed">✅ ยืนยัน</a> |
<a href="update_status.php?id=<?= $row['id'] ?>&status=cancelled">❌ ยกเลิก</a>
</td>
</tr>
<?php endwhile; ?>
</table>
📌 6. อัปเดตสถานะการนัดหมาย (update_status.php
)
<?php
include 'db.php';
$id = $_GET['id'];
$status = $_GET['status'];
$conn->query("UPDATE appointments SET status = '$status' WHERE id = $id");
header("Location: admin_appointments.php");
?>
✅ สรุป
💡 ระบบจองนัดหมาย ที่มีฟีเจอร์ครบครัน:
- ลูกค้าจองนัดหมายผ่านหน้าเว็บ
- เลือกวันที่สะดวกผ่าน Datepicker
- บันทึกข้อมูลลงฐานข้อมูล
- แจ้งเตือนผ่าน LINE Notify
- แอดมินสามารถจัดการนัดหมายได้