-
Notifications
You must be signed in to change notification settings - Fork 0
/
server1.php
352 lines (276 loc) · 11.9 KB
/
server1.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
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
<?php
session_start();
$_SESSION['conn'] = "";
$admin_id = "";
$errors = array();
// connect to the database
$db = mysqli_connect("localhost", "root", "", "lib");
$_SESSION['conn'] = $db;
$title = "";
$ISBN = "";
$publisher = "";
$edition = " ";
$Author = " ";
$Avail=" ";
if (isset($_POST['editbooks'])) {
// receive all input values from the form
$title = mysqli_real_escape_string($db, $_POST['title']);
$ISBN = mysqli_real_escape_string($db, $_POST['ISBN']);
$publisher = mysqli_real_escape_string($db, $_POST['publisher']);
$edition = mysqli_real_escape_string($db, $_POST['edition']);
$Author = mysqli_real_escape_string($db, $_POST['Author']);
$Avail = mysqli_real_escape_string($db, $_POST['Avail']);
$reftype = $_POST['reftype'];
$ttype = $_POST['ttype'];
if (empty($title)) {
array_push($errors, "Book title is required");
}
if (empty($ISBN)) {
array_push($errors, "ISBN is required");
}
// first check the database to make sure
// a book with same isbn doesnt exist
$user_check_query = "SELECT * FROM book WHERE ISBN='" . $ISBN . "' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if book exists
// $query = "INSERT INTO students"." (username, name, email, Pass,type)
// VALUES"."('$username','$name', '$email', '$password','$type')";
$sql = "UPDATE book SET title = '$title', Author= '$Author',Avail='$Avail', publisher='$publisher',edition='$edition', ref_flag='$reftype',t_flag='$ttype' WHERE ISBN = ' " . $ISBN . " '";
// $query = "UPDATE book " . "SET title = '$title', Author= '$Author', publisher='$publisher',edition='$edition', ref_flag='$reftype,t_flag='$ttype'" . "WHERE ISBN = ' " . $ISBN . " '";
// // $query = "INSERT INTO book" . "(ISBN, title, Author,publisher ,edition, ref_flag, t_flag) VALUES" . "('$ISBN', '$title', '$Author','$publisher','$edition', '$reftype', '$ttype')";
mysqli_query($db, $sql);
echo "book added to db";
array_push($errors, $title);
header('location: admin_home.php');
}else{
array_push($errors, "ISBN does not match");
}
}
/*------------------------------Edting profile to the database--------------- */
if (isset($_POST['update'])) {
$Phone = $_POST['Phone'];
$Name = $_POST['Name'];
$Email = $_POST['email'];
// // form validation: ensure that the form is correctly filled ...
// // by adding (array_push()) corresponding error unto $errors array
if (empty($Name)) {
array_push($errors, "Name is required");
}
if (count($errors) == 0) {
$sql = "UPDATE admins SET admin_name = '$Name', admin_email= '$Email', phone_number='$Phone' WHERE admin_id = ' " . $admin_id . " '";
// $query = "UPDATE admins " . "SET admin_name = '$Name', admin_emai= '$Email', phone_number='$Phone'" . "WHERE admin_id = ' " . $admin_id . " '";
mysqli_query($db, $sql);
array_push($errors, "$Name");
}
}
/*------------------------------updating password to the admin of database--------------- */
if (isset($_POST['change'])) {
$old = mysqli_real_escape_string($db, $_POST['old']);
$New = mysqli_real_escape_string($db, $_POST['New']);
$New1 = mysqli_real_escape_string($db, $_POST['New1']);
if (empty($old)) {
array_push($errors, "Old password is required");
}
if (empty($New)) {
array_push($errors, "Password is required");
}
if ($New != $New1) {
array_push($errors, "The two passwords do not match");
}
if (
count($errors) == 0
) {
// $pswd = md5($user_pswd);
// $query = "SELECT * FROM users WHERE user_Fname='$user_Fname' AND user_pswd='$user_pswd'";
// $results = mysqli_query($db, $query);
$sql_query = "select count(*) as cntUser from admins where admin_id='" . $admin_id . "' and admin_pswd='" . $old . "'";
// $result = mysqli_query($db, $sql_query);
$row = mysqli_fetch_array(mysqli_query($db, $sql_query));
$count = $row['cntUser'];
if ($count > 0) {
$query = "UPDATE admins " . "SET admin_pswd = '$New'" . "WHERE admin_id = ' " . $admin_id . " '";
mysqli_query($db, $query);
$_SESSION['success'] = "You are now logged in";
header('location: admin_home.php ');
} else {
array_push($errors, "Old password is incorrect");
}
}
}
/*------------------------------adding author to the database--------------- */
// $Auth_name=" ";
// if (isset($_POST['addAuthor'])) {
// $Auth_name = mysqli_real_escape_string($db, $_POST['Auth_name']);
// if (empty($Auth_name)) {
// array_push($errors, "Name is required");
// }
// if (count($errors) == 0) {
// // $query = "INSERT INTO students"." (username, name, email, Pass,type)
// // VALUES"."('$username','$name', '$email', '$password','$type')";
// $query = "INSERT INTO author" . "( Auth_name) VALUES" . "( '$Auth_name')";
// mysqli_query($db, $query);
// echo "Author added to db";
// header('location: admin_home.php');
// }
// }
/*------------------------------adding books to the database--------------- */
// initializing variables
$title = "";
$ISBN = "";
$publisher = "";
$edition = " ";
$Author=" ";
if (isset($_POST['addBooks'])) {
// receive all input values from the form
$title = mysqli_real_escape_string($db, $_POST['title']);
$ISBN = mysqli_real_escape_string($db, $_POST['ISBN']);
$publisher = mysqli_real_escape_string($db, $_POST['publisher']);
$edition = mysqli_real_escape_string($db, $_POST['edition']);
$Author = mysqli_real_escape_string($db, $_POST['Author']);
$reftype = $_POST['reftype'];
$ttype = $_POST['ttype'];
if (empty($title)) {
array_push($errors, "Book title is required");
}
if (empty($ISBN)) {
array_push($errors, "ISBN is required");
}
// first check the database to make sure
// a book with same isbn doesnt exist
$user_check_query = "SELECT * FROM book WHERE ISBN='" . $ISBN . "' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if book exists
array_push($errors, "ISBN (book) already exists");
}
if (count($errors) == 0) {
// $query = "INSERT INTO students"." (username, name, email, Pass,type)
// VALUES"."('$username','$name', '$email', '$password','$type')";
$query = "INSERT INTO book" . "(ISBN, title, Author,publisher ,edition, ref_flag, t_flag) VALUES" . "('$ISBN', '$title', '$Author','$publisher','$edition', '$reftype', '$ttype')";
mysqli_query($db, $query);
echo "book added to db";
header('location: admin_home.php');
}
}
/* ------------------------------end of adding books--------------------------*/
/*--------------------------------adding copies------------------------------ */
// initializing variables
$ISBN1 = "";
$Cid ="";
if (isset($_POST['addcopy'])) {
// receive all input values from the form
$ISBN1 = mysqli_real_escape_string($db, $_POST['ISBN']);
$Cid = mysqli_real_escape_string($db, $_POST['Cid']);
if (empty($ISBN1)) {
array_push($errors, "Book ISBN is required");
}
if (empty($Cid)) {
array_push($errors, "Copyid is required");
}
// first check the database to make sure
// a book with same isbn doesnt exist
$user_check_query = "SELECT * FROM book WHERE ISBN='" . $ISBN1 . "' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if (!$user) { // if book exists
array_push($errors, "ISBN of (book) does not exists, first add book to database");
}
$user_check_query1 = "SELECT * FROM copy WHERE ISBN='" . $ISBN1 . "' and C_id = '" . $Cid . "'LIMIT 1";
$result1 = mysqli_query($db, $user_check_query1);
$user1 = mysqli_fetch_assoc($result1);
if ($user1) { // if book exists
array_push($errors, "copy already exists!");
}
if (count($errors) == 0) {
// $query = "INSERT INTO students"." (username, name, email, Pass,type)
// VALUES"."('$username','$name', '$email', '$password','$type')";
$query = "INSERT INTO copy" . "(ISBN, C_id,purchase_date) VALUES" . "('$ISBN1', '$Cid', now())";
mysqli_query($db, $query);
echo "book added to db";
header('location: admin_home.php');
}
}
/*--------------------------------end of adding copies------------------------------ */
// <!-- -------------------------------------------login for admin------------------------------------------ -->
if (isset($_POST['admin_loginuser'])) {
$admin_id = mysqli_real_escape_string($db, $_POST['admin_id']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($admin_id)) {
array_push($errors, "admin_id is required");
}
if (empty($password)) {
array_push(
$errors,
"Password is required"
);
}
if (
count($errors) == 0
) {
// $pswd = md5($user_pswd);
// $query = "SELECT * FROM users WHERE user_Fname='$user_Fname' AND user_pswd='$user_pswd'";
// $results = mysqli_query($db, $query);
$sql_query = "select count(*) as cntUser from admins where admin_id='" . $admin_id . "' and admin_pswd='" . $password . "'";
// $result = mysqli_query($db, $sql_query);
$row = mysqli_fetch_array(mysqli_query($db, $sql_query));
$count = $row['cntUser'];
if ($count > 0) {
$_SESSION['admin_id'] = $admin_id;
$_SESSION['success'] = "You are now logged in";
header('location: admin_home.php ');
} else {
array_push($errors, "Wrong_id/password combination");
}
}
}
$id='';
$name='';
$email='';
$use='';
if (isset($_POST['addins'])) {
// receive all input values from the form
$use = mysqli_real_escape_string($db, $_POST['use']);
$name = mysqli_real_escape_string($db, $_POST['name']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password = mysqli_real_escape_string($db, $_POST['pas']);
$type = $_POST['type'];
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($use)) {
array_push($errors, "Username is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check = "SELECT * FROM instructor WHERE instructor_email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check);
$user = mysqli_fetch_assoc($result);
$user_check_query1 = "SELECT * FROM instructor WHERE instructor_username='$use' LIMIT 1";
$result1 = mysqli_query($db, $user_check_query1);
$user1 = mysqli_fetch_assoc($result1);
if ($user) { // if user exists
if ($user['instructor_email'] === $email) {
array_push($errors, "email already exists");
}
}
if ($user1) { // if user exists
if ($user['instructor_username'] === $use) {
array_push($errors, "username 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
$query = "INSERT INTO instructor" . " ( `instructor_name`, `instructor_email`, `instructor_username`, `instructor_pass`)
VALUES" . "('$name', '$email', '$use','$password')";
mysqli_query($db, $query);
header('location: admin_home.php');
}
}
?>