-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_booking.php
77 lines (67 loc) · 2.45 KB
/
process_booking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
// process_booking.php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Collect and sanitize data
$FullName = trim($_POST['FullName']);
$Email = trim($_POST['Email']);
$Phone = trim($_POST['Phone']);
$Address = trim($_POST['Address']);
$ServiceID = intval($_POST['ServiceID']);
$PreferredDateTime = $_POST['PreferredDateTime'];
$Message = trim($_POST['Message']);
// Validate required fields
if (empty($FullName) || empty($Email) || empty($Phone) || empty($Address) || empty($ServiceID) || empty($PreferredDateTime)) {
// Handle error
echo "Please fill in all required fields.";
exit;
}
// Validate email format
if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format.";
exit;
}
// Validate PreferredDateTime is in the future
if (strtotime($PreferredDateTime) <= time()) {
echo "Preferred Date & Time must be in the future.";
exit;
}
// Check if customer exists
$stmt = $conn->prepare("SELECT CustomerID FROM customers WHERE Email = ?");
$stmt->bind_param("s", $Email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Customer exists
$row = $result->fetch_assoc();
$CustomerID = $row['CustomerID'];
} else {
// Insert new customer
$stmt = $conn->prepare("INSERT INTO customers (FullName, Email, Phone, Address) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $FullName, $Email, $Phone, $Address);
if ($stmt->execute()) {
$CustomerID = $conn->insert_id;
} else {
// Handle error
echo "Error inserting customer: " . $stmt->error;
exit;
}
}
// Insert booking
$stmt = $conn->prepare("INSERT INTO bookings (CustomerID, ServiceID, PreferredDateTime, Message, Status) VALUES (?, ?, ?, ?, 'Pending')");
$stmt->bind_param("iiss", $CustomerID, $ServiceID, $PreferredDateTime, $Message);
if ($stmt->execute()) {
$BookingID = $conn->insert_id;
// Success message
echo "<script>alert('Booking successful! Your booking ID is " . $BookingID . "'); window.location.href = 'contact.php';</script>";
} else {
// Handle error
echo "Error inserting booking: " . $stmt->error;
exit;
}
// Close the statement
$stmt->close();
}
// Close the database connection
$conn->close();
?>