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

// Sanitize and validate inputs
$birthday_date = trim($_POST['birthday_date'] ?? '');
$name = trim($_POST['name'] ?? '');
$detail = trim($_POST['detail'] ?? '');

if (empty($birthday_date) || empty($name) || empty($detail)) {
    die('All fields are required.');
}

if (strlen($name) > 20) {
    die('Name must be 10 characters or less.');
}

// Insert into database
$stmt = $mysqli->prepare("INSERT INTO birthday_tb (birthday_date, name, detail) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $birthday_date, $name, $detail);

if ($stmt->execute()) {
    // ✅ Redirect to manage page after success
    header("Location: manage_birthday.php");
    exit;
} else {
    echo "Error: " . $stmt->error;
}

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