<?php
include('../dbconnect.php');

if (isset($_GET['id'])) {
    $id = intval($_GET['id']);

    // Get image path before deleting
    $query = "SELECT photo FROM photos WHERE id = $id";
    $result = mysqli_query($con, $query);
    $row = mysqli_fetch_assoc($result);
    $file_path = $row['photo'];

    // Delete file from server
    if (file_exists($file_path)) {
        unlink($file_path);
    }

    // Delete from database
    $del_query = "DELETE FROM photos WHERE id = $id";
    if (mysqli_query($con, $del_query)) {
        echo "<script>alert('Photo deleted successfully!'); window.location.href='gallery.php';</script>";
    } else {
        echo "Error deleting record: " . mysqli_error($con);
    }
}
?>
