AIPT/cms_admin/product_delete.php

47 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2024-10-29 11:42:53 +08:00
<?php
// Include the database connection file
include('container/security.php');
// Check if the product ID is provided
if (isset($_GET['id'])) {
// Retrieve product ID from the URL
$id = intval($_GET['id']);
// Check if the ID is valid
if ($id > 0) {
// Fetch product data to get the icon and image paths
$result = mysqli_query($conn, "SELECT icon, image FROM products WHERE id = $id");
if ($result && mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$icon_path = 'assets/images/product_icons/' . basename($row['icon']);
$image_path = 'assets/images/product_images/' . basename($row['image']);
// Prepare SQL DELETE statements
$delete_product_sql = "DELETE FROM products WHERE id = $id";
// Execute the SQL statement
if (mysqli_query($conn, $delete_product_sql)) {
// Remove the product icon and image from the directory
if (file_exists($icon_path)) {
unlink($icon_path);
}
if (file_exists($image_path)) {
unlink($image_path);
}
echo "<script>alert('Product deleted successfully.'); window.location.href = 'product_view.php';</script>";
} else {
echo "<script>alert('Error: " . mysqli_error($conn) . "'); window.location.href = 'product_view.php';</script>";
}
} else {
echo "<script>alert('Product not found.'); window.location.href = 'product_view.php';</script>";
}
} else {
echo "<script>alert('Invalid product ID.'); window.location.href = 'product_view.php';</script>";
}
} else {
echo "<script>alert('No product ID provided.'); window.location.href = 'product_view.php';</script>";
}
mysqli_close($conn);
?>