first commit
This commit is contained in:
45
cms_admin/category_delete.php
Normal file
45
cms_admin/category_delete.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
// Include the database connection file
|
||||
include('container/security.php');
|
||||
|
||||
if (isset($_GET['id'])) {
|
||||
// Retrieve category ID from the URL
|
||||
$id = intval($_GET['id']);
|
||||
|
||||
// Check if the ID is valid
|
||||
if ($id > 0) {
|
||||
|
||||
// Fetch the category data to get the icon path
|
||||
$result = mysqli_query($conn, "SELECT c_icon FROM categories WHERE id = $id");
|
||||
if ($result && mysqli_num_rows($result) > 0) {
|
||||
$row = mysqli_fetch_assoc($result);
|
||||
$icon_path = $row['c_icon'];
|
||||
|
||||
// Prepare the SQL DELETE statements
|
||||
$delete_category_sql = "DELETE FROM categories WHERE id = $id";
|
||||
$delete_products_sql = "DELETE FROM products WHERE category_id = $id";
|
||||
|
||||
// Execute the SQL statements
|
||||
if (mysqli_query($conn, $delete_products_sql) && mysqli_query($conn, $delete_category_sql)) {
|
||||
// Remove the category icon from the directory
|
||||
if (file_exists($icon_path)) {
|
||||
unlink($icon_path);
|
||||
}
|
||||
echo "<script>alert('Category and related products deleted successfully.'); window.location.href = 'category_add.php';</script>";
|
||||
} else {
|
||||
echo "<script>alert('Error: " . mysqli_error($conn) . "'); window.location.href = 'category_add.php';</script>";
|
||||
}
|
||||
} else {
|
||||
echo "<script>alert('Category not found.'); window.location.href = 'category_add.php';</script>";
|
||||
}
|
||||
} else {
|
||||
echo "<script>alert('Invalid category ID.'); window.location.href = 'category_add.php';</script>";
|
||||
}
|
||||
} else {
|
||||
echo "<script>alert('No category ID provided.'); window.location.href = 'category_add.php';</script>";
|
||||
}
|
||||
|
||||
// Close the database connection
|
||||
mysqli_close($conn);
|
||||
?>
|
||||
Reference in New Issue
Block a user