-
Notifications
You must be signed in to change notification settings - Fork 1
/
userborrow.php
261 lines (221 loc) · 9.25 KB
/
userborrow.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
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login_reader.php");
exit;
}
$issue_err = "";
require 'connect_db.php';
if($_SERVER["REQUEST_METHOD"]=="POST") {
$issueID = test_input($_POST["issueBookID"]);
if($issueID==NULL) {
echo "<br>Search field is empty<br>";
}
else {
require 'connect_db.php';
$userID = $_SESSION["id"];
$bookID = $issueID;
$action = "Borrow";
$date1 = date("Y-m-d");
$time1 = date("h:i:s");
$date_temp = strtotime(date('Y-m-d'));
$due_date = date('Y-m-d',strtotime('+15 days',$date_temp));
// we should check if the book is available first. if not show message.
$checkQue = "SELECT availability FROM book where book_ID = ?";
if($stmt = mysqli_prepare($link, $checkQue)){
mysqli_stmt_bind_param($stmt, "s", $param_bookID);
$param_bookID = $bookID;
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1) {
mysqli_stmt_bind_result($stmt, $status);
if(mysqli_stmt_fetch($stmt)){
if($status == 'Available') {
//insert into REPORT table
$insert_reportQue = "INSERT INTO report(reader_ID, book_ID, action, date, time) VALUES('".$userID."','". $bookID."','".$action."','".$date1."','".$time1."')";
//update 'Available' to 'Borrowed'
$updateQue = "UPDATE book SET availability='Borrowed' where book_ID='".$bookID."'";
//insert into BORROWED table
$insert_borrowQue = "INSERT INTO borrowed VALUES('".$userID."','".$bookID."','".$date1."','".$due_date."')";
//select email_id of reader
$email_Que = "SELECT ";
//execute queries
//inserting into BORROWED table
if ($link->query($insert_borrowQue) === TRUE) {
//echo "New record created successfully";
//we do nothing here
} else {
echo "Error: " . $insert_borrowQue . "<br>" . $link->error;
}
//inserting into REPORT table
if ($link->query($insert_reportQue) === TRUE) {
//echo "New record created successfully";
//we do nothing here
} else {
echo "Error: " . $insert_reportQue . "<br>" . $link->error;
}
//updating the BOOK table
if ($link->query($updateQue) === TRUE) {
//echo "New record created successfully";
//we do nothing here
} else {
echo "Error: " . $updateQue . "<br>" . $link->error;
}
echo "<script>alert('Book issued successfully');document.location='homeuser.php'</script>";
//javascript alert code here
//header("Location: welcome_reader.php");
//exit;
} else if($status == 'Available_Reserved'){
//check if this is the user who reserved the book
$check_reserveQue = "SELECT * FROM reserved WHERE bookID='".$bookID."' AND userID='".$userID."'";
$result = $link->query($check_reserveQue);
if ($result->num_rows > 0) {
// this user did reserve the book. we need to issue the book to him
//insert into REPORT table
$insert_reportQue = "INSERT INTO report(reader_ID, book_ID, action, date, time) VALUES('".$userID."','". $bookID."','".$action."','".$date1."','".$time1."')";
//update 'Reserved' to 'Borrowed'
$updateQue = "UPDATE book SET availability='Borrowed' where book_ID='".$bookID."'";
//insert into BORROWED table
$insert_borrowQue = "INSERT INTO borrowed VALUES('".$userID."','".$bookID."','".$date1."','".$due_date."')";
//delete from RESERVE table
$delete_reserveQue = "DELETE FROM reserved WHERE bookID='".$bookID."' AND reader_ID='".$userID."'";
//select email_id of reader
$email_Que = "SELECT ";
//execute queries
//inserting into BORROWED table
if ($link->query($insert_borrowQue) === TRUE) {
//echo "New record created successfully";
//we do nothing here
} else {
echo "Error: " . $insert_borrowQue . "<br>" . $link->error;
}
//inserting into REPORT table
if ($link->query($insert_reportQue) === TRUE) {
//echo "New record created successfully";
//we do nothing here
} else {
echo "Error: " . $insert_reportQue . "<br>" . $link->error;
}
//updating the BOOK table
if ($link->query($updateQue) === TRUE) {
//echo "New record created successfully";
//we do nothing here
} else {
echo "Error: " . $updateQue . "<br>" . $link->error;
}
//deleting from RESERVE table
if ($link->query($delete_reserveQue) === TRUE) {
//echo "New record created successfully";
//we do nothing here
} else {
echo "Error: " . $delete_reserveQue . "<br>" . $link->error;
}
echo "<script>alert('Book issued successfully. You had reserved it earlier');document.location='homeuser.php'</script>";
} else {
//this user did not reserve the book
echo "<script>alert('Book is temporarily unavailable');</script>";
$issue_err = "Book is temporarily unavailable";
}
} else {
echo "<script>alert('Book is temporarily unavailable');</script>";
$issue_err = "Book is temporarily unavailable";
}
}
} else {
echo "<script>alert('Book not found');</script>";
$issue_err = "Book not found";
}
}
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<html>
<head>
<title>Biblio@DSCE</title>
<script src="https://kit.fontawesome.com/5d3eee0a99.js" crossorigin="anonymous"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="homeu.css">
<style type="text/css">
.page-content .partitiona{
width:50%;
float:left;
padding: 10px 30px;
/* border-right: 1px solid gray; */
}
.page-content .partitionb{
width:50%;
float:right;
padding: 10px 30px;
}
</style>
</head>
<body>
<div class="vertical-nav bg-dark text-light" id="sidebar">
<div class="py-4 px-3 mb-4 bg-dark text-light">
<div class="media d-flex align-item-center">
<img src="avatar1.png" alt="user image" width="80" height="80" class="mr-3 rounded-circle img-thumbnail shadow-sm">
<div class="media-body">
<h4 class="mt-3"> <?php echo htmlspecialchars($_SESSION["username"]); ?> </h4>
<p class="font-weight-normal text-muted mb-0">STUDENT</p>
</div>
</div>
</div>
<p class="text-gray font-weight-bold text-uppercase px-3 small pb-4 mb-0">Dashboard</p>
<ul class="nav flex-column bg-white mb-0">
<li class="nav-item">
<a href="homeuser.php" class="nav-link active bg-dark text-light"><i class="fa fa-th-large mr-3 text-primary fa-fw"></i>home</a>
</li>
<li class="nav-item">
<a href="usersearch.php" class="nav-link bg-dark text-light"><i class="fas fa-search mr-3 text-primary fa-fw"></i>search book</a>
</li>
<li class="nav-item">
<a href="usermybooks.php" class="nav-link bg-dark text-light"><i class="fas fa-book-reader mr-3 text-primary fa-fw"></i>reserved books</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link bg-dark text-dark" id="highlight"><i class="fas fa-book-open mr-3 text-primary fa-fw"></i>borrow book</a>
</li>
<li class="nav-item">
<a href="userreturn.php" class="nav-link bg-dark text-light"><i class="fas fa-book mr-3 text-primary fa-fw"></i>return book</a>
</li>
</ul>
<p class="text-gray font-weight-bold text-uppercase px-3 small py-5 mb-0"></p>
<p class="text-gray font-weight-bold text-uppercase px-3 small py-5 mb-0"></p>
<ul class="nav flex-column bg-white mb-0">
<li class="nav-item">
<a href="#" class="nav-link bg-dark text-light"><i class="fas fa-exchange-alt mr-3 text-primary fa-fw"></i>my profile</a>
</li>
<li class="nav-item">
<a href="logout_reader.php" class="nav-link bg-dark text-light"><i class="fas fa-sign-out-alt mr-3 text-primary fa-fw"></i>logout</a>
</li>
</ul>
</div>
<div class="page-content p-5" id="content">
<h1>Borrow Book</h1><br/>
<div class="partition">
<?php
if(!empty($login_err)){
echo '' . $issue_err . '<br/>';
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Book ID</label>
<input type="number" class="form-control" id="exampleFormControlInput1" placeholder="Enter Book ID" id="issueID" name="issueBookID" required>
</div>
<button type="submit" class="btn btn-primary" name="issueSubmit" value="Issue">Issue Book</button>
</form>
</div>
</div>
</body>
</html>