-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuspects_data.php
454 lines (392 loc) · 16.3 KB
/
suspects_data.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
session_start();
if (!isset($_SESSION['name'])) {
header('Location: index.php');
exit();
}
// Include the database connection
include('./Includes/connection.php');
// Function to get suspects from the database
function getSuspects($conn)
{
$suspectStmt = $conn->prepare("SELECT * FROM suspect_details");
$suspectStmt->execute();
$suspects = $suspectStmt->get_result();
$suspectStmt->close();
return $suspects;
}
// Check if the add suspect form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_suspect'])) {
// Extract form data
$suspect_id = $_POST['suspect_id'];
$name = $_POST['name'];
$age = $_POST['age'];
$address = $_POST['address'];
$image = $_POST['image'];
$prev_crimes = $_POST['prev_crimes'];
try {
// Prepare and execute the INSERT query
$addSuspectStmt = $conn->prepare("INSERT INTO suspect_details (suspect_id, name, age, address, image, prev_crimes)
VALUES (?, ?, ?, ?, ?, ?)");
$addSuspectStmt->bind_param('ssssss', $suspect_id, $name, $age, $address, $image, $prev_crimes);
$addSuspectStmt->execute();
$addSuspectStmt->close();
// Set a session variable for the success message
$_SESSION['suspect_added'] = true;
// Redirect to the same page to avoid resubmission on refresh
header('Location: ' . $_SERVER['PHP_SELF']);
exit();
} catch (mysqli_sql_exception $e) {
// Check if it's a duplicate entry error
if ($e->getCode() === 1062) {
$_SESSION['error_message'] = 'Criminal ID is not available';
} else {
$_SESSION['error_message'] = 'An error occurred while adding the suspect';
}
// Redirect to the same page to avoid resubmission on refresh
header('Location: ' . $_SERVER['PHP_SELF']);
exit();
}
}
// Check if there's a success message to show
$suspect_added = isset($_SESSION['suspect_added']) ? $_SESSION['suspect_added'] : false;
if ($suspect_added) {
unset($_SESSION['suspect_added']);
}
// Check if there's an error message to show
$error_message = isset($_SESSION['error_message']) ? $_SESSION['error_message'] : false;
if ($error_message) {
unset($_SESSION['error_message']);
}
// Fetch suspects using the getSuspects function
$suspects = getSuspects($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Control Panel - Manage Suspects</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.main-content {
margin: 20px;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
h2, h3 {
color: #333;
text-align: center;
}
form {
margin-bottom: 20px;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
form div {
flex: 0 0 48%;
padding: 10px;
}
label {
display: block;
margin-bottom: 5px;
color: #555;
}
input {
width: 90%;
padding: 8px;
margin-bottom: 10px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
padding: 10px;
border: none;
cursor: pointer;
border-radius: 50px;
}
button:hover {
background-color: #45a049;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #4caf50;
color: #fff;
}
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
a {
color: #d9534f;
text-decoration: none;
cursor: pointer;
}
a:hover {
text-decoration: underline;
}
.export-btn {
background-color: #337ab7;
color: #fff;
padding: 10px;
border: none;
cursor: pointer;
border-radius: 5px;
}
.export-btn:hover {
background-color: #286090;
}
/* Modal styles */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
padding-top: 60px;
}
.modal-content {
background-color: #fefefe;
margin: 5% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 600px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
.modal-form {
display: flex;
flex-direction: column;
}
.modal-form input {
margin-bottom: 15px;
width: 100%;
}
.modal-form button {
align-self: center;
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
cursor: pointer;
border-radius: 5px;
}
.modal-form button:hover {
background-color: #0056b3;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
<?php if ($suspect_added): ?>
alert('New criminal added');
<?php endif; ?>
<?php if ($error_message): ?>
alert('<?php echo addslashes($error_message); ?>');
<?php endif; ?>
});
function exportToExcel(tableId) {
var tab_text = "<table border='2px'><tr>";
var textRange; var j = 0;
var tab = document.getElementById(tableId); // id of table
for (j = 0; j < tab.rows.length; j++) {
tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
}
tab_text = tab_text + "</table>";
tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); // remove if you want links in your table
tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if you want images in your table
tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // removes input params
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) { // If Internet Explorer
var txtArea1 = document.createElement('iframe');
txtArea1.style.display = 'none';
document.body.appendChild(txtArea1);
txtArea1.document.open("txt/html", "replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
txtArea1.document.execCommand("SaveAs", true, "Criminals_List.xls");
} else {
var link = document.createElement('a');
link.href = 'data:application/vnd.ms-excel,' + encodeURIComponent(tab_text);
link.download = 'SuspectsList.xls';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
function openEditModal(suspectId) {
fetch('get_suspect.php?id=' + suspectId)
.then(response => response.json())
.then(data => {
document.getElementById('edit-suspect_id').value = data.suspect_id;
document.getElementById('edit-name').value = data.name;
document.getElementById('edit-age').value = data.age;
document.getElementById('edit-address').value = data.address;
document.getElementById('edit-image').value = data.image;
document.getElementById('edit-prev_crimes').value = data.prev_crimes;
document.getElementById('edit-modal').style.display = 'block'; // Show edit modal
});
}
function closeEditModal() {
document.getElementById('edit-modal').style.display = 'none'; // Hide edit modal
}
function deleteSuspect(suspectId) {
if (confirm('Are you sure you want to delete this criminal?')) {
fetch('delete_suspect.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'id=' + encodeURIComponent(suspectId)
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
alert(data.message);
window.location.href = 'suspects_data.php';
} else {
alert(data.message);
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while deleting the suspect.');
});
}
}
</script>
</head>
<body>
<div class="main-content">
<a href="#" onclick="window.history.back(); return false;"><i style="font-size:24px; color:blue;" class="fa"></i></a>
<a href="dashboard.php"><i style="font-size:24px;color:blue;" class="fa"></i></a>
<h2>Manage Criminals</h2>
<form method="post" action="" style="border: 1px solid #ddd; padding: 20px; border-radius: 5px;">
<div>
<label for="suspect_id">Criminal ID:</label>
<input type="text" name="suspect_id" placeholder="Enter criminal ID" required>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Enter name" required>
<label for="address">Address:</label>
<input type="text" name="address" placeholder="Enter address" required>
</div>
<div>
<label for="image">Image:</label>
<input type="text" name="image" placeholder="Enter image file path" required>
<label for="age">Age:</label>
<input type="text" name="age" placeholder="Enter age" required>
<label for="prev_crimes">Previous Crimes:</label>
<input type="text" name="prev_crimes" placeholder="Enter previous crimes" required>
</div>
<div style="text-align: center; margin: 5px auto;">
<button type="submit" name="add_suspect" onclick="return confirm('Are you sure about adding the criminal?')">Add Criminal</button>
</div>
</form>
<!-- Edit Modal -->
<div id="edit-modal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeEditModal()">×</span>
<h2>Edit Criminals</h2>
<form id="edit-suspect-form" method="post" action="update_suspect.php" class="modal-form">
<input type="hidden" id="edit-suspect_id" name="suspect_id">
<label for="edit-name">Name:</label>
<input type="text" id="edit-name" name="name" required>
<label for="edit-age">Age:</label>
<input type="text" id="edit-age" name="age" required>
<label for="edit-address">Address:</label>
<input type="text" id="edit-address" name="address" required>
<label for="edit-image">Image:</label>
<input type="text" id="edit-image" name="image" required>
<label for="edit-prev_crimes">Previous Crimes:</label>
<input type="text" id="edit-prev_crimes" name="prev_crimes" required>
<button type="submit" style="background-color: #007bff; color: #fff; padding: 10px 20px; border: none; cursor: pointer; border-radius: 5px;">Save</button>
</form>
</div>
</div>
<div>
<h2>Criminals List</h2>
<!-- Export to Excel button -->
<button class="export-btn" onclick="exportToExcel('suspect-table')">Export to Excel</button>
<table id="suspect-table">
<thead>
<tr>
<th>Criminal ID</th>
<th>Name</th>
<th>Age</th>
<th>Address</th>
<th>Image</th>
<th>Previous Crimes</th>
<th>Actions</th> <!-- Added Actions Column -->
</tr>
</thead>
<tbody>
<?php while ($row = $suspects->fetch_assoc()) { ?>
<tr>
<td><?php echo htmlspecialchars($row['suspect_id']); ?></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['age']); ?></td>
<td><?php echo htmlspecialchars($row['address']); ?></td>
<td>
<?php
$imagePath = 'Suspects/' . $row['image'];
if (file_exists($imagePath)) {
echo '<img src="' . htmlspecialchars($imagePath) . '" alt="Image" style="max-width: 100px; max-height: 100px;">';
} else {
$parentImagePath = './' . $row['image']; // Images in parent directory
if (file_exists($parentImagePath)) {
echo '<img src="' . htmlspecialchars($parentImagePath) . '" alt="Image" style="max-width: 100px; max-height: 100px;">';
} else {
echo 'Image not found';
}
}
?>
</td>
<td><?php echo htmlspecialchars($row['prev_crimes']); ?></td>
<td>
<button style="background-color: #007bff; color: #fff; padding: 5px 10px; border: none; border-radius: 5px; cursor: pointer;" onclick="openEditModal('<?php echo htmlspecialchars($row['suspect_id']); ?>')">Edit</button>
<button style="background-color: #dc3545; color: #fff; padding: 5px 10px; border: none; border-radius: 5px; cursor: pointer;" onclick="deleteSuspect('<?php echo htmlspecialchars($row['suspect_id']); ?>')">Delete</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</body>
</html>