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

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

    // Check if notice already exists
    $checkQuery = "SELECT * FROM notice WHERE n_name = '$n_name'";
    $result = mysqli_query($con, $checkQuery);

    if (mysqli_num_rows($result) > 0) {
        // Update the existing notice (you can update other fields if present)
        $updateQuery = "UPDATE notice SET n_name = '$n_name' WHERE n_name = '$n_name'";
        if (mysqli_query($con, $updateQuery)) {
            echo '<script>window.location.href = "notice.php";</script>';
            exit();
        } else {
            echo "Error while updating: " . mysqli_error($con);
        }
    } else {
        // Insert new notice (assuming `id` is auto-increment)
        $insertQuery = "INSERT INTO notice (n_name) VALUES ('$n_name')";
        if (mysqli_query($con, $insertQuery)) {
            echo '<script>window.location.href = "notice.php";</script>';
            exit();
        } else {
            echo "Error while inserting: " . mysqli_error($con);
        }
    }
}
?>