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

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

    // Check for duplicate subject name (excluding current record)
    $checkQuery = "SELECT * FROM subject WHERE s_name = '$s_name' AND id != '$id'";
    $result = mysqli_query($con, $checkQuery);

    if (mysqli_num_rows($result) > 0) {
        echo "Duplicate entry found: Subject name already exists.";
    } else {
        $updateQuery = "UPDATE subject SET s_name = '$s_name' WHERE id = '$id'";

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