-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_component.php
40 lines (34 loc) · 1.25 KB
/
delete_component.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
<?php
// Start session and include database connection
session_start();
include 'connectDB.php';
// Check if ID is posted and is a valid numeric value
if (isset($_POST['id']) && is_numeric($_POST['id'])) {
$component_id = $_POST['id'];
// Start transaction
$conn->begin_transaction();
try {
// Delete from child table first
$stmt = $conn->prepare("DELETE FROM transactions WHERE component_id = ?");
$stmt->bind_param("i", $component_id);
$stmt->execute();
$stmt->close();
// Now delete the main inventory component
$stmt = $conn->prepare("DELETE FROM inventory WHERE component_id = ?");
$stmt->bind_param("i", $component_id);
if ($stmt->execute()) {
echo "success"; // Send a success response
$conn->commit(); // Commit the transaction
} else {
throw new Exception("Error deleting component: " . $conn->error);
}
$stmt->close();
} catch (Exception $e) {
$conn->rollback(); // Rollback the transaction in case of any error
echo $e->getMessage(); // Send the error message as the response
}
$conn->close();
} else {
echo "Invalid component ID.";
}
?>