-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
302 lines (256 loc) · 11.2 KB
/
admin.php
File metadata and controls
302 lines (256 loc) · 11.2 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
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "web";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle Create (Add User)
if (isset($_POST['add_user'])) {
$username = $_POST['username'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT); // Password hashing
$phone = $_POST['phone'];
$role = $_POST['role'];
$uploadDir = 'uploads/';
// Handle profile photo upload
if (isset($_FILES['profile_photo']) && $_FILES['profile_photo']['error'] == 0) {
$profilePhotoName = $_FILES['profile_photo']['name'];
$profilePhotoTmpName = $_FILES['profile_photo']['tmp_name'];
$profilePhotoPath = $uploadDir . uniqid() . '_' . basename($profilePhotoName);
move_uploaded_file($profilePhotoTmpName, $profilePhotoPath);
}
// Handle additional file upload
if (isset($_FILES['additional_file']) && $_FILES['additional_file']['error'] == 0) {
$additionalFileName = $_FILES['additional_file']['name'];
$additionalFileTmpName = $_FILES['additional_file']['tmp_name'];
$additionalFilePath = $uploadDir . uniqid() . '_' . basename($additionalFileName);
move_uploaded_file($additionalFileTmpName, $additionalFilePath);
}
// Insert the data into the database using prepared statements to prevent SQL injection
$stmt = $conn->prepare("INSERT INTO users (name, email, password, phone, role, profile_photo, additional_file)
VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssss", $username, $email, $password, $phone, $role, $profilePhotoPath, $additionalFilePath);
if ($stmt->execute()) {
header("Location: admin.php");
exit();
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
// Handle Update (Edit User)
if (isset($_POST['edit_user'])) {
$user_id = $_POST['user_id']; // Make sure to get user_id
$username = $_POST['username'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT); // Password hashing
$phone = $_POST['phone'];
$role = $_POST['role'];
$uploadDir = 'uploads/';
// Handle profile photo upload
if (isset($_FILES['profile_photo']) && $_FILES['profile_photo']['error'] == 0) {
$profilePhotoName = $_FILES['profile_photo']['name'];
$profilePhotoTmpName = $_FILES['profile_photo']['tmp_name'];
$profilePhotoPath = $uploadDir . uniqid() . '_' . basename($profilePhotoName);
move_uploaded_file($profilePhotoTmpName, $profilePhotoPath);
}
// Handle additional file upload
if (isset($_FILES['additional_file']) && $_FILES['additional_file']['error'] == 0) {
$additionalFileName = $_FILES['additional_file']['name'];
$additionalFileTmpName = $_FILES['additional_file']['tmp_name'];
$additionalFilePath = $uploadDir . uniqid() . '_' . basename($additionalFileName);
move_uploaded_file($additionalFileTmpName, $additionalFilePath);
}
// Update the data in the database using prepared statements to prevent SQL injection
$stmt = $conn->prepare("UPDATE users SET name=?, email=?, password=?, phone=?, role=?, profile_photo=?, additional_file=? WHERE user_id=?");
$stmt->bind_param("sssssssi", $username, $email, $password, $phone, $role, $profilePhotoPath, $additionalFilePath, $user_id);
if ($stmt->execute()) {
header("Location: admin.php");
exit();
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
// Handle Delete (Delete User)
if (isset($_GET['delete_id'])) {
$user_id = $_GET['delete_id'];
// Delete the user from the database using prepared statement
$stmt = $conn->prepare("DELETE FROM users WHERE user_id = $user_id");
$stmt->bind_param("i", $user_id);
if ($stmt->execute()) {
echo "User deleted successfully!";
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
// Fetch users to display in the table
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result === FALSE) {
die("Error fetching users: " . $conn->error);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Page - Hotel Management System</title>
<link rel="stylesheet" href="admin.css" />
</head>
<body>
<nav class="navbar">
<p>Hotel Management System - Admin Page</p>
<div>
<a href="index.php">Log Out</a>
</div>
</nav>
<main>
<div class="add-user-container">
<button id="addUserButton" onclick="toggleAddUserForm()">Add User</button>
</div>
<div id="addUserForm" class="user-form" style="display: none;">
<h3>Add New User</h3>
<form action="admin.php" method="POST" enctype="multipart/form-data">
<div class="form-field">
<input type="text" name="username" placeholder="Username" required>
</div>
<div class="form-field">
<input type="email" name="email" placeholder="Email" required>
</div>
<div class="form-field">
<input type="password" name="password" placeholder="Password" required>
</div>
<div class="form-field">
<input type="tel" name="phone" placeholder="Phone Number" required pattern="[0-9]{10}" title="Please enter a valid 10-digit phone number">
</div>
<div class="form-field">
<label for="role">Role:</label>
<select name="role" id="role">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="form-field">
<input type="file" name="profile_photo" accept="image/*" id="profilePhoto" required>
</div>
<div class="form-field">
<input type="file" name="additional_file" accept=".pdf,.doc,.docx,.txt" id="additionalFile" required>
</div>
<div class="form-field">
<button type="submit" name="add_user" class ="button">Add User</button>
<button type="button" class ="button" onclick="toggleAddUserForm()">Cancel</button>
</div>
</form>
</div>
<h2 class="infotable">Users Information</h2>
<table>
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Phone Number</th>
<th>Role</th>
<th>Profile Photo</th>
<th>Additional File</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['role']; ?></td>
<td><img src="<?php echo $row['profile_photo']; ?>" alt="Profile Photo" width="100px"></td>
<td><a href="<?php echo $row['additional_file']; ?>" download>Download Additional File</a></td>
<td>
<a href="admin.php?delete_id=<?php echo $row['user_id']; ?>" onclick="return confirm('Are you sure you want to delete this user?')" style="text-decoration: none">
<button class = "button" >Delete</button>
</a>
<button class ="button" onclick="editUser(
<?php echo $row['user_id']; ?>,
'<?php echo addslashes($row['name']); ?>',
'<?php echo addslashes($row['email']); ?>',
'<?php echo addslashes($row['phone']); ?>',
'<?php echo addslashes($row['role']); ?>',
'<?php echo addslashes($row['PASSWORD']); ?>'
)">Edit</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div id="editUserForm" class="user-form" style="display: none;">
<h3>Edit User</h3>
<form action="admin.php" method="POST">
<input type="hidden" name="user_id" id="editUserId">
<div class="form-field">
<input type="text" name="username" id="editUsername" placeholder="Username" required>
</div>
<div class="form-field">
<input type="email" name="email" id="editEmail" placeholder="Email" required>
</div>
<div class="form-field">
<input type="password" name="password" id="editPassword" placeholder="Password">
</div>
<div class="form-field">
<input type="tel" name="phone" id="editPhone" placeholder="Phone Number" required pattern="[0-9]{10}" title="Please enter a valid 10-digit phone number">
</div>
<div class="form-field">
<input type="file" name="profile_photo" accept="image/*" id="profilePhoto" placeholder="Image" required>
</div>
<div class="form-field">
<input type="file" name="additional_file" accept=".pdf,.doc,.docx,.txt" id="additionalFile" placeholder="File" required>
</div>
<div class="form-field">
<label for="role">Role:</label>
<select name="role" id="editRole">
<option value="user">User</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="form-field">
<button type="submit" name="edit_user" class ="button">Save Changes</button>
<button type="button" class ="button" onclick="toggleEditUserForm()">Cancel</button>
</div>
</form>
</div>
</main>
<script>
function toggleAddUserForm() {
var form = document.getElementById('addUserForm');
if (form.style.display === 'none' || form.style.display === '') {
form.style.display = 'block';
} else {
form.style.display = 'none';
}
}
function toggleEditUserForm() {
var form = document.getElementById('editUserForm');
if (form.style.display === 'none' || form.style.display === '') {
form.style.display = 'block';
} else {
form.style.display = 'none';
}
}
function editUser(id, username, email, phone, role, password) {
document.getElementById('editUserId').value = id;
document.getElementById('editUsername').value = username;
document.getElementById('editPassword').value = password;
document.getElementById('editEmail').value = email;
document.getElementById('editPhone').value = phone;
document.getElementById('editRole').value = role;
toggleEditUserForm();
}
</script>
</body>
</html>
<?php
$conn->close();
?>