<?php
include "config.php"; // Ensure this connects to your DB

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $b_name = mysqli_real_escape_string($con, $_POST['b_name']);

    // Check for existing bank name
    $checkQuery = "SELECT * FROM bank WHERE b_name = '$b_name'";
    $result = mysqli_query($con, $checkQuery);

    if (mysqli_num_rows($result) > 0) {
        // Update the existing record (if there's something to update)
        // Example: just updating the same name to trigger an update timestamp, or update other fields as needed
        $updateQuery = "UPDATE bank SET b_name = '$b_name' WHERE b_name = '$b_name'";
        if (mysqli_query($con, $updateQuery)) {
            echo "Bank name already existed, updated the existing record.";
            // You can redirect or echo success message
            echo '<script>window.location.href = "bank.php";</script>';
        } else {
            echo "Error while updating: " . mysqli_error($con);
        }
    } else {
        // Insert new bank name
        $query = "INSERT INTO bank (b_name) VALUES ('$b_name')";
        if (mysqli_query($con, $query)) {
            echo "New bank name inserted successfully.";
            echo '<script>window.location.href = "bank.php";</script>';
        } else {
            echo "Error while inserting: " . mysqli_error($con);
        }
    }
}
?>