-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditReversation.php
More file actions
210 lines (182 loc) · 7.99 KB
/
EditReversation.php
File metadata and controls
210 lines (182 loc) · 7.99 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
<?php
session_start();
// Check if the user is logged in
$is_logged_in = isset($_SESSION['username']);
$userType = $is_logged_in ? $_SESSION['userType'] : null;
$userName = $is_logged_in ? $_SESSION['username'] : null;
if (!$is_logged_in) {
header("Location: login.php");
exit();
}
// Handle logout
if (isset($_GET['logout'])) {
session_unset();
session_destroy();
header("Location: index.php");
exit();
}
$passworddb = "";
$UserID = $BookID = $ReturnDate = ""; // Initialize variables
// Check if `ReversationID` is provided in the URL
if (isset($_GET['ReversationID'])) {
$ReversationID = $_GET['ReversationID'];
// Connect to the database
$connection = mysqli_connect("localhost", "root", $passworddb, "library");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Retrieve the reversation data
$query = "SELECT * FROM `reversation` WHERE `ReversationID` = ?";
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "i", $ReversationID);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
$UserID = $row['UserID'];
$BookID = $row['BookID'];
$ReturnDate = $row['ReturnDate'];
} else {
echo "Reversation not found.";
exit();
}
mysqli_stmt_close($stmt);
// Fetch all users for the UserID dropdown
$users_query = "SELECT `UserID`, `UserName` FROM `user`";
$users_result = mysqli_query($connection, $users_query);
// Fetch all books for the BookID dropdown
$books_query = "SELECT `BookID`, `Title` FROM `book`";
$books_result = mysqli_query($connection, $books_query);
mysqli_close($connection);
} else {
die("ReversationID is required.");
}
// Handle form submission to update reversation data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$UserID = $_POST["UserID"];
$BookID = $_POST["BookID"];
$ReturnDate = $_POST["ReturnDate"];
// Connect to the database
$connection = mysqli_connect("localhost", "root", $passworddb, "library");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Update reversation data
$update_query = "UPDATE `reversation` SET `UserID` = ?, `BookID` = ?, `ReturnDate` = ? WHERE `ReversationID` = ?";
$stmt = mysqli_prepare($connection, $update_query);
if ($stmt) {
mysqli_stmt_bind_param($stmt, "sssi", $UserID, $BookID, $ReturnDate, $ReversationID);
if (mysqli_stmt_execute($stmt)) {
// Redirect to the reservation page with a success message
header("Location: reservation.php?status=success");
exit();
} else {
echo "Error updating reversation: " . mysqli_stmt_error($stmt);
}
mysqli_stmt_close($stmt);
} else {
echo "Error preparing statement: " . mysqli_error($connection);
}
mysqli_close($connection);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit ReversationID</title>
<link rel="stylesheet" href="css/editReversationStyle.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.1/css/all.min.css" integrity="sha512-5Hs3dF2AEPkpNAR7UiOHba+lRSJNeM2ECkwxUIxC1Q/FLycGTbNapWXB4tP889k5T5Ju8fs4b1P5z/iB4nMfSQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<header class="header">
<div class="logo">
<a href="index.php"><i class="fa-solid fa-book"></i> Knowledge Nest</a>
</div>
<nav class="nav-bar">
<ul class="nav__links">
<li><a href="index.php">Home</a></li>
<li><a href="#CSection">Contact</a></li>
<li><a href="books.php">Books</a></li>
<?php if ($userType === 'admin') { ?>
<li><a href="borrowBook.php">Borrow</a></li>
<li><a href="reservation.php">Reservation</a></li>
<?php } ?>
<?php if ($is_logged_in): ?>
<?php if ($userType === 'user'): ?>
<li><a href="myAccount.php">My Account</a></li>
<?php elseif ($userType === 'admin'): ?>
<li><a href="dashboard.php">Admin Dashboard</a></li>
<?php endif; ?>
<?php endif; ?>
</ul>
</nav>
<div class="login">
<?php if ($is_logged_in): ?>
<form method="GET" action="index.php">
<button type="submit" name="logout">
<i class="fa-solid fa-sign-out-alt"></i><b class="logout-text">Logout</b>
</button>
</form>
<?php else: ?>
<a href="login.php" class="login-icon">
<button><i class="fa-solid fa-user"></i><b class="login-text">Login</b></button>
</a>
<?php endif; ?>
</div>
</header>
<main>
<div class="dashboard-container">
<a href="javascript:history.back()" class="back-button" >
<i class="fa-solid fa-arrow-left"></i> Back
</a>
<div class="dashboard-content">
<h2>Edit Reversation</h2>
<form action="EditReversation.php?ReversationID=<?php echo htmlspecialchars($ReversationID); ?>" method="POST">
<table>
<tr>
<td>UserID:</td>
<td>
<select name="UserID" required>
<?php
if (isset($users_result) && mysqli_num_rows($users_result) > 0) {
while ($user = mysqli_fetch_assoc($users_result)) {
$selected = ($user['UserID'] == $UserID) ? 'selected' : '';
echo "<option value='" . htmlspecialchars($user['UserID']) . "' $selected>" . htmlspecialchars($user['UserName']) . "</option>";
}
} else {
echo "<option value=''>No users available</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>BookID:</td>
<td>
<select name="BookID" required>
<?php
if (isset($books_result) && mysqli_num_rows($books_result) > 0) {
while ($book = mysqli_fetch_assoc($books_result)) {
$selected = ($book['BookID'] == $BookID) ? 'selected' : '';
echo "<option value='" . htmlspecialchars($book['BookID']) . "' $selected>" . htmlspecialchars($book['Title']) . "</option>";
}
} else {
echo "<option value=''>No books available</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>ReturnDate:</td>
<td><input type="date" name="ReturnDate" value="<?php echo htmlspecialchars($ReturnDate); ?>" required></td>
</tr>
</table>
<button type="submit">Update Reversation</button>
</form>
</div>
</div>
</main>
</body>
</html>