<?php
include("connection.php");

if(isset($_GET['id'])){

    $id = mysqli_real_escape_string($con, $_GET['id']);

    // Fetch record first
    $sql = "SELECT * FROM locations WHERE id='$id'";
    $query = mysqli_query($con,$sql);
    $row = mysqli_fetch_assoc($query);

    if($row){

        $old_image = $row['Image'];

        // Delete record
        $delete = "DELETE FROM locations WHERE id='$id'";
        $query1 = mysqli_query($con,$delete);

        if($query1){

            // Delete image if exists
            $image_path = "./assets/images/location/".$old_image;

            if(!empty($old_image) && file_exists($image_path)){
                unlink($image_path);
            }

            header("Location: view_location.php");
            exit();

        }else{
            echo "Delete failed: ".mysqli_error($con);
        }

    }else{
        echo "Record not found.";
    }

}else{
    echo "Invalid request.";
}
?>