-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.php
165 lines (130 loc) · 4.77 KB
/
delete.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
<!-- ----------------------- PHP ----------------------- -->
<?php
// Start the session to retrieve session data
session_start();
// Set database credentials
$host = "localhost";
$username = $_SESSION['username'];
$password = $_SESSION['password'];
$database = "crime";
// creating a connection object
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); // if connection fails, print.
}
// php variables
$selectedAppealId = "";
$deleteButtonPressed = false;
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["delete_button_pressed"])) {
// Get values from the form
$selectedAppealId = $_POST["selectedAppealId"];
// Start transaction refernce: https://www.php.net/manual/en/pdo.transactions.php
$conn->begin_transaction();
try {
// Delete statement
$sqlDelete = "DELETE FROM appeals WHERE appeal_ID = ?";
$stmtDelete = $conn->prepare($sqlDelete);
$stmtDelete->bind_param("i", $selectedAppealId);
$stmtDelete->execute();
// Check if delete was successful
if ($stmtDelete->affected_rows > 0) {
echo "<p>Record deleted successfully.</p>";
// Commit the transaction
$conn->commit();
} else {
echo "<p>Error deleting record: " . $stmtDelete->error . "</p>";
// Rollback the transaction in case of error
$conn->rollback();
}
$stmtDelete->close();
} catch (Exception $e) {
// Rollback the transaction in case of error
$conn->rollback();
echo "<p>Error: " . $e->getMessage() . "</p>";
}
}
?>
<!-- ----------------------- HTML ----------------------- -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:opsz,wght@9..40,400;9..40,500&family=DM+Serif+Display&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<title>Delete Page</title>
</head>
<body>
<div class="top">
<h2 class="profileTitle">Delete Appeal</h2>
<a href="crime_search.php" class = "searchBar">SEARCH</a>
</div>
<?php
if ($deleteButtonPressed) {
// use prepared statement to delete the entry
$sqlDelete = "DELETE FROM appeals WHERE appeal_id = ?";
$stmtDelete = $conn->prepare($sqlDelete);
$stmtDelete->bind_param("s", $selectedAppealId);
if ($stmtDelete->execute()) {
echo "<p style='text-align: center; color: green;'>Record deleted successfully</p>";
} else {
echo "<p style='text-align: center; color: red;'>Error deleting record: " . $stmtDelete->error . "</p>";
}
$stmtDelete->close();
}
?>
<!-- select appeal ID -->
<div class="request">
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<label>Select Appeal ID :</label>
<br>
<select name="selectedAppealId">
<option value="">-- Select Appeal ID --</option>
<?php
$sqlAppeals = "SELECT appeal_id FROM appeals";
$resultAppeals = $conn->query($sqlAppeals);
if ($resultAppeals) {
while ($rowAppeals = $resultAppeals->fetch_assoc()) {
echo "<option value='" . $rowAppeals["appeal_id"] . "'>" . $rowAppeals["appeal_id"] . "</option>";
}
}
?>
</select>
<br>
<br>
<input type="submit" name="delete_button_pressed" value="Delete">
</form>
</div>
<!-- Display appeals table -->
<div>
<?php
$sql = "SELECT * FROM appeals";
$result = $conn->query($sql);
$attributes = [];
if ($result) {
echo "<table>";
// column headers
echo "<tr>";
while ($column = $result->fetch_field()) {
echo "<th>" . $column->name . "</th>";
array_push($attributes, $column->name);
}
// all Rows
while ($row = $result->fetch_assoc()) {
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
} else {
echo "No results.";
}
?>
</div>
</body>
</html>