<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}
require 'dbconn.php';

// Ensure form is submitted via POST and 'id' exists
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
    $id          = intval($_POST['id']);
    $title       = trim($_POST['news_title']);
    $date        = trim($_POST['news_date']);
    $short_desc  = trim($_POST['short_desc']);
    $long_desc   = trim($_POST['long_desc']);

    // Validate required fields
    if ($title === '' || $date === '' || $short_desc === '' || $long_desc === '') {
        die("All fields are required.");
    }

    // Prepare update query
    $query = "UPDATE news_tb SET 
                news_title = ?, 
                news_date = ?, 
                short_desc = ?, 
                long_desc = ? 
              WHERE id = ?";

    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("ssssi", $title, $date, $short_desc, $long_desc, $id);

    if ($stmt->execute()) {
        header("Location: manage_news.php?success=1");
        exit;
    } else {
        echo "Error updating record: " . $stmt->error;
    }

    $stmt->close();
} else {
    echo "Invalid request.";
}
?>
