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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $spid     = mysqli_real_escape_string($con, $_POST['spid']);
    $name     = mysqli_real_escape_string($con, $_POST['name']);
    $address  = mysqli_real_escape_string($con, $_POST['address']);
    $landline = mysqli_real_escape_string($con, $_POST['landline']);
    $number   = mysqli_real_escape_string($con, $_POST['number']);
    $email    = mysqli_real_escape_string($con, $_POST['email']);

    // Check for duplicate by name, mobile number, or email
    $checkQuery = "SELECT * FROM policestation WHERE p_name = '$name' OR p_number = '$number' OR p_email = '$email'";
    $result = mysqli_query($con, $checkQuery);

    if (mysqli_num_rows($result) > 0) {
        echo "Duplicate entry found: Station name, mobile number, or email already exists.";
    } else {
        $query = "INSERT INTO policestation (spid, p_name, p_address, p_landline, p_number, p_email)
                  VALUES ('$spid', '$name', '$address', '$landline', '$number', '$email')";

        if (mysqli_query($con, $query)) {
                                    echo '<script>window.location.href = "police_station.php";</script>';
        } else {
            echo "Error: " . mysqli_error($con);
        }
    }
}
?>