AIPT/cms_admin/cn_blog_delete.php

38 lines
1.4 KiB
PHP
Raw Normal View History

2024-10-29 11:42:53 +08:00
<?php
// Include the database connection file
include('container/security.php');
if (isset($_GET['id'])) {
$blog_id = mysqli_real_escape_string($conn, $_GET['id']);
// Fetch the blog details to get the image filename
$query = "SELECT image FROM cn_blogs WHERE id='$blog_id'";
$result = mysqli_query($conn, $query);
if ($result && mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$image_name = $row['image'];
$target_dir = "assets/images/blogs_image/";
$target_file = $target_dir . $image_name;
// Delete the blog record from the database
$delete_query = "DELETE FROM cn_blogs WHERE id='$blog_id'";
if (mysqli_query($conn, $delete_query)) {
// Delete the image file from the directory
if (file_exists($target_file)) {
unlink($target_file);
}
echo "<script>alert('Blog deleted successfully.'); window.location.href = 'cn_blog_view';</script>";
} else {
echo "<script>alert('Error: " . mysqli_error($conn) . "'); window.location.href = 'cn_blog_view';</script>";
}
} else {
echo "<script>alert('Blog not found.'); window.location.href = 'cn_blog_view';</script>";
}
mysqli_close($conn);
} else {
echo "<script>alert('Invalid request.'); window.location.href = 'cn_blog_view';</script>";
}
?>