<?php
session_start();
include("dbconfig.php");


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get the Patient ID from the form
    $patid = $_POST['patid'];

    // Validate input
    if (empty($patid)) {
        echo "Patient ID is required.";
        exit;
    }

    // Prepare the delete query for 'bill' table
    $deleteBillQuery = "DELETE FROM `bill` WHERE `PatientId` = ?";
    $stmt = $conn->prepare($deleteBillQuery);
    $stmt->bind_param("s", $patid);
    $stmt->execute();

    // Check if the record was deleted
    if ($stmt->affected_rows > 0) {
        // Prepare the delete query for 'finalbill' table
        $deleteFinalBillQuery = "DELETE FROM `finalbill` WHERE `PatientId` = ?";
        $stmtFinal = $conn->prepare($deleteFinalBillQuery);
        $stmtFinal->bind_param("s", $patid);
        $stmtFinal->execute();

        if ($stmtFinal->affected_rows > 0) {
            echo "Record deleted successfully.";
        } else {
            echo "Record not found in finalbill table.";
        }
    } else {
        echo "Record not found in bill table.";
    }

    // Close statements
    $stmt->close();
    if (isset($stmtFinal)) {
        $stmtFinal->close();
    }

    // Close the database connection
    $conn->close();

    // Redirect back to the previous page or display a success message
    header("Location: index.php"); // Change this to your actual page
    exit();
}
?>

