File "add_category.php"

Full Path: /home/alphpwcp/previewstream.online/admin/add_category.php
File size: 1.15 KB
MIME-type: text/x-php
Charset: utf-8

<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}
include '../db.php';

$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name']);
    if ($name !== '') {
        $stmt = $pdo->prepare("INSERT INTO categories (name) VALUES (?)");
        if ($stmt->execute([$name])) {
            $message = "Category added successfully!";
        } else {
            $message = "Failed to add category.";
        }
    } else {
        $message = "Category name cannot be empty.";
    }
}
?>

<?php include 'menu.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <title>Add Category</title>
    <link rel="stylesheet" href="../style.css">
</head>
<body>
<div class="container">
    <h2>Add New Category</h2>
    <?php if($message): ?><p style="color:green;"><?= htmlspecialchars($message) ?></p><?php endif; ?>
    <form method="POST">
        <input type="text" name="name" placeholder="Category name" required>
        <button type="submit">Add Category</button>
    </form>
    <p><a href="dashboard.php">Back to Dashboard</a></p>
</div>
</body>
</html>