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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Sanitize and get form data
    $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
    $events_title = trim($_POST['events_title']);
    $events_date = trim($_POST['events_date']);
    $event_desc = trim($_POST['event_desc']);

    // Validate required fields
    if ($id && $events_title && $events_date && $event_desc) {
        // Prepare the update query
        $stmt = $mysqli->prepare("
            UPDATE events_tb 
            SET event_title = ?, event_date = ?, event_desc = ? 
            WHERE id = ?
        ");
        $stmt->bind_param("sssi", $events_title, $events_date, $event_desc, $id);

        if ($stmt->execute()) {
            // Success: redirect to event list or confirmation page
            header("Location: manage_events.php?status=updated");
            exit;
        } else {
            echo "Error updating event: " . $stmt->error;
        }

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