-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-product.php
32 lines (26 loc) · 969 Bytes
/
delete-product.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
<?php
include("./layouts/session.php");
include 'conn.php'; // Include database connection
// Establish the connection to the user's database
$conn = connectMainDB();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$product_id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$imageName = isset($_POST['image']) ? $_POST['image'] : '';
if ($product_id > 0) {
// Prepare SQL query to delete the product
$stmt = $conn->prepare("DELETE FROM products WHERE id = ?");
$stmt->bind_param('i', $product_id);
if ($stmt->execute()) {
// Delete the image from the uploads folder
if (!empty($imageName) && file_exists('uploads/' . $imageName)) {
unlink('uploads/' . $imageName); // Remove the file
}
echo 'success'; // Send success response
} else {
echo 'error'; // Send error response
}
$stmt->close();
} else {
echo 'error';
}
}