<?php
include "db.php";

if (isset($_POST['id'])) {
    
    $id = intval($_POST['id']);

    $get = mysqli_query($conn, "SELECT image FROM gallery WHERE id = $id");
    $row = mysqli_fetch_assoc($get);
    $image = $row['image'];

    if (!empty($image) && file_exists("../images/$image")) {
        unlink("../images/$image");
    }

    $delete = mysqli_query($conn, "DELETE FROM gallery WHERE id = $id");

    if ($delete) {
        echo json_encode(['status' => 'success']);
    } else {
        echo json_encode(['status' => 'error']);
    }
}
?>
