<?php
include 'config.php'; // Your DB connection

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['id'];
    $pid = $_POST['pid'];
    $i_name = $_POST['i_name'];
    $i_number = $_POST['i_number'];
    $i_designation = $_POST['i_designation'];

    // Handle signature file upload if exists
    if (!empty($_FILES['i_sign']['name'])) {
        $fileName = basename($_FILES['i_sign']['name']);
        $uploadDir = 'uploads/';
        $uploadPath = $uploadDir . $fileName;

        move_uploaded_file($_FILES['i_sign']['tmp_name'], $uploadPath);

        // With image update
        $sql = "UPDATE police SET 
                    pid='$pid', 
                    i_name='$i_name', 
                    i_number='$i_number', 
                    i_designation='$i_designation', 
                    i_sign='$fileName' 
                WHERE id='$id'";
    } else {
        // Without image update
        $sql = "UPDATE police SET 
                    pid='$pid', 
                    i_name='$i_name', 
                    i_number='$i_number', 
                    i_designation='$i_designation' 
                WHERE id='$id'";
    }

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