<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['vno'])) {
    $vno = $_POST['vno'];

    require 'connection.php';

    $conn=$connect;


    $stmt = $conn->prepare("SELECT slipimage FROM jvvoucher WHERE vno = ?");
    $stmt->bind_param("s", $vno);
    $stmt->execute();
    $stmt->bind_result($imagePath);
    $stmt->fetch();
    $stmt->close();

    if ($imagePath && file_exists($imagePath)) {
        if (unlink($imagePath)) {
            // Remove reference in DB
            $update = $conn->prepare("UPDATE jvvoucher SET slipimage = '' WHERE vno = ?");
            $update->bind_param("s", $vno);
            $update->execute();
            $update->close();
            echo "Image deleted successfully.";
        } else {
            echo "Failed to delete image file.";
        }
    } else {
        echo "Image file not found.";
    }

    $conn->close();
} else {
    echo "Invalid request.";
}
?>
