-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
372 lines (293 loc) · 11.9 KB
/
server.php
File metadata and controls
372 lines (293 loc) · 11.9 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
session_start();
// initializing variables
$firstname = "";
$mobile = "";
$band_name = "";
$venue_name ="";
$venue_capacity ="";
$concert_band ="";
$concert_venue ="";
$concert_id="";
$concert_age ="";
$booking_id ="";
$dob="";
$now = time();
$update = false;
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration1');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$firstname = mysqli_real_escape_string($db, $_POST['firstname']);
$lastname = mysqli_real_escape_string($db, $_POST['lastname']);
$mobile = mysqli_real_escape_string($db, $_POST['mobile']);
$dob = mysqli_real_escape_string($db, $_POST['dob']);
$password_1 = mysqli_real_escape_string($db, $_POST['passid']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($firstname)) { array_push($errors, "First Name is required"); }
if (empty($lastname)) { array_push($errors, "Last Name is required"); }
if (empty($mobile)) { array_push($errors, "Mobile number is required"); }
if (empty($dob)) { array_push($errors, "Date of birth is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same mobile number
$user_check_query = "SELECT * FROM users WHERE mobile='$mobile'";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['mobile'] === $mobile) {
array_push($errors, "User already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$dob = date('Y-m-d', strtotime($dob));
$query = "INSERT INTO users (firstname,lastname,mobile,dob,password)
VALUES('$firstname','$lastname','$mobile','$dob','$password')";
mysqli_query($db, $query);
$_SESSION["mobile"] = $mobile;
$_SESSION["firstname"] = $firstname;
header('location: events.php');
}
}
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$mobile = mysqli_real_escape_string($db, $_POST['mobile']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($mobile)) {
array_push($errors, "Mobile Number is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$sql = ("SELECT Firstname, Lastname, DOB,user_id FROM users WHERE mobile='$mobile' AND password='$password'");
if ($result = mysqli_query($db, $sql)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
$firstname= $row["Firstname"];
$lastname=$row["Lastname"];
$dob=$row["DOB"];
$user_id=$row["user_id"];
//storing session values
$_SESSION["firstname"] = $firstname;
$_SESSION["lastname"] = $lastname;
$_SESSION["dob"] = $dob;
$_SESSION["user_id"] = $user_id;
}
}
$query = "SELECT * FROM users WHERE mobile='$mobile' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION["mobile"] = $mobile;
header('location: events.php');
}else {
array_push($errors, "Wrong mobile/password combination");
}
}
}
// LOGIN Admin
if (isset($_POST['login_admin'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "User Name is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$query = "SELECT * FROM admins WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
header('location: dashboard.php');
}else {
array_push($errors, "Wrong Username/password combination");
}
}
}
//adding band
if (isset($_POST['addband'])) {
$band_name = mysqli_real_escape_string($db, $_POST['band_name']);
// form validation: ensure that the form is correctly filled
if (empty($band_name)) { array_push($errors, "Band Name is required"); }
// first check the database to make sure
// a BAND does not already exist with the same name
$query = "SELECT * FROM band WHERE band_name='$band_name'";
$result = mysqli_query($db, $query);
$band = mysqli_fetch_assoc($result);
if ($band) { // if band exists
if ($band['band_name'] === $band_name) {
array_push($errors, "band already exists");
}
}
if (count($errors) == 0) {
mysqli_query($db, "INSERT INTO band (band_name) VALUES ('$band_name')");
array_push($errors, "Band Added!");
}
}
//.....
// ... UPDATING BAND
if (isset($_POST['edit'])) {
$band_id = $_POST['band_id'];
$band_name = $_POST['band_name'];
if (empty($band_name)) { array_push($errors, "Band Name is required"); }
// first check the database to make sure
// a BAND does not already exist with the same name
$query = "SELECT * FROM band WHERE band_name='$band_name'";
$result = mysqli_query($db, $query);
$band = mysqli_fetch_assoc($result);
if ($band) { // if band exists
if ($band['band_name'] === $band_name) {
array_push($errors, "band already exists");
}
}
if (count($errors) == 0) {
mysqli_query($db, "UPDATE band SET band_name='$band_name' WHERE band_id=$band_id");
array_push($errors, "Band Updated!");
}
}
///.....DELETING BAND
if (isset($_GET['del'])) {
$band_id = $_GET['del'];
mysqli_query($db, "DELETE FROM band WHERE band_id=$band_id");
array_push($errors, "Band Deleted!");
header('location: bands.php');
}
///....
//adding venue
if (isset($_POST['addvenue'])) {
$venue_name = mysqli_real_escape_string($db, $_POST['venue_name']);
$venue_capacity = mysqli_real_escape_string($db, $_POST['venue_capacity']);
// form validation: ensure that the form is correctly filled
if (empty($venue_name)) { array_push($errors, "Venue Name is required");}
if (empty($venue_capacity)) { array_push($errors, "Venue Capacity is required"); }
// first check the database to make sure
// a venue does not already exist with the same name
$query = "SELECT * FROM venue WHERE venue_name='$venue_name'";
$result = mysqli_query($db, $query);
$venue = mysqli_fetch_assoc($result);
if ($venue) { // if venue exists
if ($venue['venue_name'] === $venue_name) {
array_push($errors, "venue already exists");
}
}
if (count($errors) == 0) {
mysqli_query($db, "INSERT INTO venue (venue_name, venue_capacity) VALUES ('$venue_name', '$venue_capacity')");
array_push($errors, "Venue added!");
}
}
//.....
// ... updating venue
if (isset($_POST['resett'])) {
$venue_id = $_POST['venue_id'];
$venue_name = $_POST['venue_name'];
$venue_capacity = $_POST['venue_capacity'];
if (empty($venue_name)) { array_push($errors, "Venue Name is required"); }
if (empty($venue_capacity)) { array_push($errors, "Venue capacity is required"); }
// first check the database to make sure
// a venue does not already exist with the same name
$query = "SELECT * FROM venue WHERE venue_name='$venue_name'";
$result = mysqli_query($db, $query);
$venue = mysqli_fetch_assoc($result);
if ($venue) { // if venue exists
if ($venue['venue_capacity'] > $venue_capacity) {
array_push($errors, "Sorry the capacity cant go lower");
}
}
if (count($errors) == 0) {
mysqli_query($db, "UPDATE venue SET venue_name='$venue_name', venue_capacity='$venue_capacity' WHERE venue_id=$venue_id");
array_push($errors, "Venue Updated!");
}
}
///.....deleting venue
if (isset($_GET['del'])) {
$venue_id = $_GET['del'];
mysqli_query($db, "DELETE FROM venue WHERE venue_id=$venue_id");
array_push($errors, "Venue Deleted!");
}
//...adding a concert
if (isset($_POST['addconcert'])) {
$concert_date = mysqli_real_escape_string($db, $_POST['concert_date']);
$concert_band = mysqli_real_escape_string($db, $_POST['concert_band']);
$concert_venue = mysqli_real_escape_string($db, $_POST['concert_venue']);
$concert_age = mysqli_real_escape_string($db, $_POST['concert_age']) ? 1 : 0;
// form validation: ensure that the form is correctly filled
if (empty($concert_date)) { array_push($errors, "Date is required"); }
if (empty($concert_band)) { array_push($errors, "Band is required"); }
if (empty($concert_venue)) { array_push($errors, "Venue is required");}
//picks band id from bands table to post in concert table
$sql = "SELECT band_id FROM band WHERE band_name='$concert_band'";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($result);
$band_id = $row["band_id"];
//picks venue id from venue table to post in concert table
$sql = "SELECT venue_id FROM venue WHERE venue_name='$concert_venue'";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($result);
$venue_id = $row["venue_id"];
$concert_date = date("Y-m-d h:i", strtotime($concert_date));
if (strtotime($concert_date) < time()) {
array_push($errors, "Date has already passed!");
}
if (count($errors) == 0) {
mysqli_query($db, "INSERT INTO concert (concert_date, band_id, venue_id,over_18) VALUES ('$concert_date', ' $band_id', '$venue_id', '$concert_age')");
array_push($errors, "Concert added!");
}
}
// booking a concert
if (isset($_GET['book'])) {
$id=$_GET['book'];
$user_id=$_SESSION['user_id'];
// first check the database to make sure
// a concert does not already exist with the same id for the same user
$query = "SELECT * FROM booking WHERE concert_id='$id' AND user_id='$user_id'";
$result = mysqli_query($db, $query);
$booking = mysqli_fetch_assoc($result);
if ($booking) { // if booking exists
if ($booking['concert_id'] === $id) {
array_push($errors, "Already booked this!");
}
}
//check if the user has two upcoming bookings
$upcomingbookings = "SELECT count('user_id'),c.concert_date FROM booking as b JOIN concert as c ON b.concert_id=c.concert_id WHERE user_id='$id'";
$result=mysqli_query($db,$upcomingbookings);
$rowk=mysqli_fetch_array($result);
if ((strtotime($rowk['concert_date']) > time()) && ($rowk > 2)) {
array_push($errors, "Maximum bookings reached");
}
//check if user is less than 18yrs
$over18="SELECT concert_id,concert_date,over_18 FROM concert WHERE over_18 = '1'";
$result=mysqli_query($db,$over18);
$over=mysqli_fetch_array($result);
$user="SELECT DATE_ADD(DOB, INTERVAL 18 YEAR) DOB,user_id FROM users WHERE user_id ='$id'";
$result=mysqli_query($db,$user);
$age=mysqli_fetch_array($result);
if((strtotime($over['concert_date']))- (strtotime($age['DOB'])) < 18){
array_push($errors,"This concert is for over 18 only!!");
}
if(count($errors)== 0){
//Making a booking
mysqli_query($db, "INSERT INTO booking (concert_id, user_id) VALUES ('$id','$user_id') ");
array_push($errors, "Concert Booked!");
}
}
/////.....cancelling a booking
if (isset($_GET['cancel'])) {
$booking_id = $_GET['cancel'];
mysqli_query($db, "DELETE FROM booking WHERE booking_id=$booking_id");
array_push($errors, "Your Booking has been cancelled!");
header('location: events.php');
}
?>