<?php require_once('header.php'); ?>

<?php
if (!isset($_REQUEST['id'])) {
    header('location: logout.php');
    exit;
}

$blog_id = $_REQUEST['id'];

$statement = $pdo->prepare("SELECT * FROM blogs WHERE id=?");
$statement->execute([$blog_id]);
$row = $statement->fetch(PDO::FETCH_ASSOC);

if (!$row) {
    header('location: logout.php');
    exit;
}

$image = $row['image'];

if (!empty($image)) {
    $image_path = '../assets/images/blog/' . $image;

    if (file_exists($image_path)) {
        unlink($image_path);
    }
}

$statement = $pdo->prepare("DELETE FROM blogs WHERE id=?");
$statement->execute([$blog_id]);

header('location: blog.php');
exit;
?>