<?php
include 'db.php'; // Database connection

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $category_id = $_POST['category_id'];

    $stmt = $pdo->prepare("INSERT INTO topics (name, category_id) VALUES (?, ?)");
    if ($stmt->execute([$name, $category_id])) {
        echo "Topic added successfully!";
    } else {
        echo "Error adding topic.";
    }
}

// Fetch all categories for the dropdown
$categories = $pdo->query("SELECT * FROM categories")->fetchAll();
?>

<form method="POST">
    <label>Topic Name: <input type="text" name="name" required></label><br>
    <label>Category:
        <select name="category_id">
            <?php foreach ($categories as $category): ?>
                <option value="<?= $category['id'] ?>"><?= $category['name'] ?></option>
            <?php endforeach; ?>
        </select>
    </label><br>
    <input type="submit" value="Add Topic">
</form>
