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

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $blog_date = $_POST['blog_date'];
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $title = trim($_POST['title']);
    $remark = $_POST['remark']; // CKEditor allows HTML

    // Simple validation
    if (empty($blog_date) || empty($name) || empty($email) || empty($title) || empty($remark)) {
        die("All fields are required.");
    }

    // Prepared statement to prevent SQL injection
    $stmt = $mysqli->prepare("INSERT INTO blogs_tb (blog_date, name, email, title, remark) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $blog_date, $name, $email, $title, $remark);

    if ($stmt->execute()) {
        // Redirect on success
        header("Location: manage_blog.php?status=success");
        exit();
    } else {
        echo "Error: " . $stmt->error;
    }

    $stmt->close();
    $mysqli->close();
}
?>
