55ok
| Direktori : /home/u307599615/domains/costumeonrent.in/public_html/admin/ |
| Current File : /home/u307599615/domains/costumeonrent.in/public_html/admin/deleteproduct.php |
<?php
include("connection.php");
// Get product ID safely
$p_id = isset($_GET['p_id']) ? intval($_GET['p_id']) : 0;
if ($p_id > 0) {
// Fetch the product
$sql = "SELECT * FROM `product` WHERE `p_id` = '$p_id'";
$query = mysqli_query($con, $sql);
if ($query && mysqli_num_rows($query) > 0) {
$row = mysqli_fetch_assoc($query);
$old_images = json_decode($row['Image'], true);
// Delete product record
$deleteSql = "DELETE FROM `product` WHERE `p_id` = '$p_id'";
$deleteQuery = mysqli_query($con, $deleteSql);
if ($deleteQuery) {
// Delete images from folder
if (!empty($old_images) && is_array($old_images)) {
foreach ($old_images as $img) {
$filePath = "./assets/images/categorie/" . $img;
if (file_exists($filePath)) {
unlink($filePath);
}
}
} elseif (!empty($row['Image'])) {
// Handle single image (non-JSON)
$filePath = "./assets/images/categorie/" . $row['Image'];
if (file_exists($filePath)) {
unlink($filePath);
}
}
// Redirect after delete
header("Location: viewproduct.php");
exit();
} else {
echo "Error deleting record: " . mysqli_error($con);
}
} else {
echo "Product not found.";
}
} else {
echo "Invalid product ID.";
}
?>