-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_machine.php
44 lines (34 loc) · 1.13 KB
/
delete_machine.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
<?php
$page = 'machine_management';
$page_title = 'Delete Machine';
include 'templates/admin_header.php';
// Check if user has admin role
if ($_SESSION['role'] !== 'admin') {
header("Location: login.php?error=access_denied");
exit();
}
// Get machine ID from URL
$machine_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($machine_id === 0) {
header("Location: machine_management.php?error=Invalid machine ID");
exit();
}
// Start transaction
$conn->begin_transaction();
try {
$stmt = $conn->prepare("DELETE FROM machine_logs WHERE machine_id = ?");
$stmt->bind_param("i", $machine_id);
$stmt->execute();
$stmt = $conn->prepare("DELETE FROM machines WHERE id = ?");
$stmt->bind_param("i", $machine_id);
$stmt->execute();
// Commit transaction
$conn->commit();
header("Location: machine_management.php?success=Machine and related logs deleted successfully");
} catch (Exception $e) {
// Rollback transaction on error
$conn->rollback();
header("Location: machine_management.php?error=" . urlencode("Error deleting machine: " . $e->getMessage()));
}
exit();
?>