-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.php
76 lines (67 loc) · 2.85 KB
/
book.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
<!--
Student Name: Frankie Zheng
Student ID: 15895990
book.php file, a server-side program that insert booking information
to the selected database.
-->
<?php
include 'connectDB.php';
if (isset($_POST['name'])&&isset($_POST['number'])&&isset($_POST['street'])&&isset($_POST['suburb'])
&&isset($_POST['time'])&&isset($_POST['destination'])&&isset($_POST['pickDate'])) {
$name = $_POST['name'];
$number = $_POST['number'];
$street = $_POST['street'];
$suburb = $_POST['suburb'];
$time = $_POST['time'];
$dest = $_POST['destination'];
$pickdate = $_POST['pickDate'];
$unit =$_POST['unit'];
//check whether unit was input, if so, add unit into pickup.
if($unit!=NULL) {
$pickup=$unit."/".$street.", ".$suburb;
}else {
$pickup=$street.", ".$suburb;
}
/*random generate unique booking reference number from
0-9 and A-Z by shuffling it randomly*/
$permitted_char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$booking_id = substr(str_shuffle($permitted_char), 0, 6);
$conn = OpenCon();
//pre-check if the booking_id is unique.
$checkId = "SELECT * FROM BOOKING WHERE BOOKING_ID = '$booking_id'";
$result = $conn->query($checkId);
//if booking_id already in database, re-assign new one
if ($result!==FALSE) {
$booking_id = substr(str_shuffle($permitted_char), 0, 6);
}
$pickuptime = $pickdate." ".$time.":00";
$currentTime = date("Y-m-d H:i:s");
//Check if the table exists in database
$exist = $conn->query("SELECT 1 FROM BOOKING");
if ($exist!==false) {
} else {
$sql_create = "CREATE TABLE BOOKING (BOOKING_ID
VARCHAR(20) PRIMARY KEY, CUSTOMER_NAME
VARCHAR(50), PHONE_NUMBER INT, PICK_UP_ADDRESS
VARCHAR(100), PICK_UP_TIME DATETIME, DESTINATION
VARCHAR(100), ASSIGN_STATUS VARCHAR(20), BOOKING_TIME DATETIME)";
$conn->query($sql_create);
}
//Pick-up time validation, return error message
if ($pickuptime>$currentTime) {
sleep(1);
$sql = "INSERT INTO BOOKING VALUES ('$booking_id', '$name','$number',
'$pickup','$pickuptime','$dest', 'Unassigned', '$currentTime')";
if ($conn->query($sql)===true) {
echo "<p>Thank you! You booking reference number is <strong style='color:red'>".$booking_id."</strong></p>";
echo "<p>You will be picked up in front of your provided address at <strong>".
$time."</strong> on <strong>".$pickdate."</strong></p>";
} else {
echo "Error: ".$sql."<br>".$conn->error;
}
} else {
sleep(1);
echo "<p>Invalid time input, please check!</p>";
}
closeCon($conn);
}