-
Notifications
You must be signed in to change notification settings - Fork 0
/
createReservations.php
266 lines (206 loc) · 9.1 KB
/
createReservations.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
// Use an HTML form to create a new entry in the Customer table.
// When SUBMIT button pressed, open new PDO (PHP data object) connection,
// then send INSERT SQL statement with the users inputted values
if (isset($_POST['create'])) {
// config.php holds the server information
// change config file to local host (the one you made in Hazra's tutorial)
// common.php maintains special characters used in html that would otherwise
// not be recognized as HTML, by calling method escape(<html>)
require "config.php";
require "common.php";
try {
//open connection with information from config.php
$connection = new PDO($dsn, $username, $password, $options);
// create variables from users form inputs. In PHP, values are placed
// into $_POST array
$new_user = array(
"ReservationNo" => $_POST['ReservationNo'],
"RoomNo" => $_POST['RoomNo'],
"CustomerID"=>$_POST['CustomerID'],
"CheckInDate"=>$_POST['CheckInDate'],
"CheckOutDate" => $_POST['CheckOutDate'],
// "FacilityName" => $_POST['FacilityName'],
// "Name" => $_POST['Name'],
// "Position" => $_POST['Position']
);
// create an SQL statement to insert users input
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"Reservation_Makes",
implode(", ", array_keys($new_user)),
":" . implode(", :", array_keys($new_user))
);
// send the SQL to the server
$statement = $connection->prepare($sql);
$statement->execute($new_user);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else if (isset($_POST['view'])){
try {
require "config.php";
require "common.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT *
FROM Reservation_Makes";
$statement = $connection->prepare($sql);
$statement->bindParam(':ReservationNo', $ReservationNo, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
if ($result && $statement->rowCount() > 0) {
echo "<table><tr><th class='border-class'>ReservationNo</th>
<th class='border-class'>RoomNo</th>
<th class='borderclass'>CustomerID</th>
<th class='borderclass'>CheckInDate</th>
<th class='borderclass'>CheckOutDate</th></tr>";
// output data of each row
foreach($result as $row) {
echo "<tr><td class='borderclass'>".$row["ReservationNo"]."</td>
<td class='borderclass'>".$row["RoomNo"]."</td>
<td class='borderclass'>".$row["CustomerID"]."</td>
<td class='borderclass'>".$row["CheckInDate"]."</td>
<td class='borderclass'>".$row["CheckOutDate"]."</td></tr>";}
echo "</table>";
} else {
echo "0 results";
}
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else if (isset($_POST['delete'])) {
try {
require "config.php";
require "common.php";
$connection = new PDO($dsn, $username, $password, $options);
$ReservationNo = $_POST['ReservationNo_del'];
$sql = "DELETE FROM Reservation_Makes WHERE ReservationNo = $ReservationNo";
$statement = $connection->prepare($sql);
$statement->bindValue(':ReservationNo_del', $ReservationNo);
$statement->execute();
$success = "User successfully deleted";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else if (isset($_POST['update'])) {
try {
require "config.php";
require "common.php";
$connection = new PDO($dsn, $username, $password, $options);
$ReservationNo = $_POST['ReservationNoUp'];
$RoomNo = $_POST['RoomNoUp'];
$OldRoomNo = $_POST['ReservationNoUp'];
$sql = "UPDATE Reservation_Makes SET RoomNo = $RoomNo WHERE ReservationNo = $OldRoomNo";
$statement = $connection->prepare($sql);
$statement->bindValue(':ReservationNo_del', $ReservationNo);
$statement->execute();
$success = "User successfully deleted";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else if (isset($_POST['join'])) {
try {
require "config.php";
require "common.php";
$connection = new PDO($dsn, $username, $password, $options);
$ReservationNo = $_POST['ReservationNo'];
// $RoomNo = $_POST['RoomNoUp'];
// $OldRoomNo = $_POST['ReservationNoUp'];
$sql = "SELECT r.Price, res.ReservationNo FROM Room r, Reservation_Makes res WHERE r.RoomNo = res.RoomNo";
$statement = $connection->prepare($sql);
$statement->bindValue(':ReservationNo', $ReservationNo);
$statement->execute();
$result = $statement->fetchAll();
if ($result && $statement->rowCount() > 0) {
echo "<table><tr><th class='border-class'>Price</th>
<th class='border-class'>ReservationNo</th></tr>";
// output data of each row
foreach($result as $row) {
echo "<tr><td class='borderclass'>".$row["Price"]."</td>
<td class='borderclass'>".$row["ReservationNo"]."</td></tr>";}
echo "</table>";
} else {
echo "0 results";
}
// $success = "User successfully deleted";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else if(isset($_POST['nested'])) {
try {
require "config.php";
require "common.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT CustomerID
FROM Customer WHERE CustomerID IN
(SELECT c.CustomerID FROM Customer c, Reservation_Makes r, Room ro
WHERE r.CustomerID = c.CustomerID AND r.RoomNo = ro.RoomNo
GROUP BY CustomerID
HAVING SUM(ro.Price) > 200)";
$statement = $connection->prepare($sql);
$statement->bindParam(':ReservationNo', $ReservationNo, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
if ($result && $statement->rowCount() > 0) {
echo "<table><tr><th class='border-class'>CustomerID</th></tr>";
// output data of each row
foreach($result as $row) {
echo "<tr><td class='borderclass'>".$row["CustomerID"]."</td></tr>";}
echo "</table>";
} else {
echo "0 results";
}
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<!-- include website title/headers/etc, a "successfully added" message,
and the input form itself.-->
<?php include "templates/header.php"; ?>
<?php if (isset($_POST['Create Reservation']) && $statement) { ?>
> <?php echo $_POST['ReservationNo']; ?> successfully added.
<?php } ?>
<h2 style="color:white;">Reservation</h2>
<form method="post">
<p>
<input type="submit" name = "view" value="View Reservations"></p>
<p><input type="submit" name = "join" value="Join Room Price and ReservationNo"></p>
<p><input type="submit" name = "nested" value="Customers spending more than 200"></p>
<p>
<label for="ReservationNo">Reservation Number</label>
<input type="text" name="ReservationNo" id="ReservationNo">
<label for="RoomNo">RoomNo</label>
<input type="text" name="RoomNo" id="RoomNo">
<label for="CustomerID">CustomerID</label>
<input type="text" name="CustomerID" id="CustomerID">
<label for="CheckInDate">Check-In Date</label>
<input type="text" name="CheckInDate" id="CheckInDate">
<label for="CheckOutDate">Check-Out Date</label>
<input type="text" name="CheckOutDate" id="CheckOutDate">
<input type="submit" name="create" value="Create Reservation"></p>
<p>
<!---->
<label for="ReservationNoUp">Reservation to Update</label>
<input type="text" name="ReservationNoUp" id="ReservationNoUp">
<label for="RoomNoUp">New RoomNo</label>
<input type="text" name="RoomNoUp" id="RoomNoUp">
<!---->
<!-- <label for="CustomerIDUp">CustomerIDUp to Update</label>-->
<!-- <input type="text" name="CustomerIDUp" id="CustomerIDUp">-->
<!---->
<!-- <label for="CheckInDateUp">CheckInDate to Update</label>-->
<!-- <input type="text" name="CheckInDateUp" id="CheckInDateUp">-->
<!---->
<!-- <label for="CheckOutDateUp">CheckOutDate to Update</label>-->
<!-- <input type="text" name="CheckOutDateUp" id="CheckOutDateUp">-->
<input type="submit" name = "update" value="Update Reservation">
</p>
<p>
<label for="ReservationNo_del">Reservation to Delete</label>
<input type="text" name="ReservationNo_del" id="ReservationNo_del">
<input type="submit" name = "delete" value="Delete Reservation">
</p>
</form>
<a href="indexCustomer.php">Back to Customer Management</a>
<?php include "templates/footer.php"; ?>