38 lines
1.4 KiB
PHP
38 lines
1.4 KiB
PHP
<?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 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 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 = 'blog_view';</script>";
|
|
} else {
|
|
echo "<script>alert('Error: " . mysqli_error($conn) . "'); window.location.href = 'blog_view';</script>";
|
|
}
|
|
} else {
|
|
echo "<script>alert('Blog not found.'); window.location.href = 'blog_view';</script>";
|
|
}
|
|
|
|
mysqli_close($conn);
|
|
} else {
|
|
echo "<script>alert('Invalid request.'); window.location.href = 'blog_view';</script>";
|
|
}
|
|
?>
|