-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_user.php
More file actions
48 lines (39 loc) · 1.36 KB
/
delete_user.php
File metadata and controls
48 lines (39 loc) · 1.36 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
<?php
// Include database connection
include 'db_connect.php';
// Get user ID from URL
$user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($user_id > 0) {
// Start transaction
$conn->begin_transaction();
try {
// First, delete user's participant associations
$stmt = $conn->prepare("DELETE FROM user_participants WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
// Delete user's log entries
$stmt = $conn->prepare("DELETE FROM log_entries WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
// Finally, delete the user
$stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
// Commit transaction
$conn->commit();
// Redirect with success message
header("Location: admin_dashboard.php?message=User+deleted+successfully");
exit();
} catch (Exception $e) {
// Rollback transaction on error
$conn->rollback();
header("Location: admin_dashboard.php?error=Error+deleting+user:+" . urlencode($e->getMessage()));
exit();
}
} else {
// Invalid user ID
header("Location: admin_dashboard.php?error=Invalid+user+ID");
exit();
}
$conn->close();
?>