-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreserve.php
54 lines (48 loc) · 1.54 KB
/
reserve.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
<html>
<head>
</head>
<?php
$party_size = $_REQUEST['party_size'];
$buid = $_REQUEST['business_unit'];
$reserved_time = $_REQUEST['reserved_time'];
$mysqli = mysqli_connect('koodemo.cwmhshxpuljc.us-west-2.rds.amazonaws.com',
'koomaster',
'koopassword',
'koodb');
if ($mysqli->connect_errno) {
echo 'Failed to connect to Mysql: ' . $mysqli->error
. ' (' . $mysqli->connect_errno . ')';
return;
}
$stmt = 'SELECT id FROM bu_tables t WHERE t.bu_id=' . $buid
. ' AND t.capacity>=' . $party_size
. ' AND t.id NOT IN'
. ' (SELECT table_id FROM reservation r WHERE r.bu_id=' . $buid
. ' AND ADDTIME(r.reserved_time, "1:00:00")>="' . $reserved_time . '"'
. ' AND ADDTIME("' . $reserved_time . '", "1:00:00")>=r.reserved_time)'
. ' ORDER BY t.capacity LIMIT 1';
$result = $mysqli->query($stmt);
if ($result && $result->num_rows == 1) {
// Found one table
$row = $result->fetch_array();
$table_id = $row['id'];
$result->close();
// Mark the table is reserved
$stmt = 'INSERT INTO reservation SET bu_id=' . $buid
. ', table_id=' . $table_id
. ', reserved_time="' . $reserved_time . '"';
if ($mysqli->query($stmt) === TRUE) {
echo $table_id;
} else {
echo 'fail-to-reserve';
}
$mysqli->close();
return;
}
// Did not find a table, clean up
if ($result) {
$result->close();
}
$mysqli->close();
echo 'no-table';
?>