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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
    $id        = intval($_POST['id']);
    $blog_date = $_POST['blog_date'] ?? '';
    $name      = $_POST['name'] ?? '';
    $email     = $_POST['email'] ?? '';
    $title     = $_POST['title'] ?? '';
    $remark    = $_POST['remark'] ?? '';

    // Basic validation
    if ($blog_date && $name && $email && $title && $remark) {
        $stmt = $mysqli->prepare("UPDATE blogs_tb SET blog_date = ?, name = ?, email = ?, title = ?, remark = ? WHERE id = ?");
        $stmt->bind_param("sssssi", $blog_date, $name, $email, $title, $remark, $id);

        if ($stmt->execute()) {
            header("Location: manage_blog.php?msg=updated");
            exit;
        } else {
            echo "Update failed: " . $stmt->error;
        }
    } else {
        echo "All fields are required.";
    }
} else {
    echo "Invalid request.";
}
?>
