-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_unit.php
52 lines (45 loc) · 1.86 KB
/
delete_unit.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
<?php
session_start();
include 'conn.php'; // database connection
// Establish the connection to the database
$conn = connectMainDB();
// Check if the request is an AJAX POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Sanitize and get the ID
$unit_id = isset($_POST['id']) ? intval($_POST['id']) : 0;
// Check if unit_id is valid
if ($unit_id > 0) {
// Prepare the SQL statement to delete the unit
$sql = "DELETE FROM units WHERE id = ? AND user_email = ?";
// Assuming you have the user's email in the session
$user_email = $_SESSION['email'];
// Prepare and bind
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("is", $unit_id, $user_email);
// Execute the statement
if ($stmt->execute()) {
// Check if any row was affected
if ($stmt->affected_rows > 0) {
// Respond with success
echo json_encode(['success' => true, 'message' => 'Unit deleted successfully.']);
} else {
// No rows affected, unit might not exist
echo json_encode(['success' => false, 'message' => 'No such unit found.']);
}
} else {
// Execution failed
echo json_encode(['success' => false, 'message' => 'Error executing delete query.']);
}
$stmt->close();
} else {
// Statement preparation failed
echo json_encode(['success' => false, 'message' => 'Error preparing delete query.']);
}
} else {
// Invalid unit ID
echo json_encode(['success' => false, 'message' => 'Invalid unit ID.']);
}
} else {
// Not an AJAX request
echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
}