
<?php include("../functions.php"); ?>

<?php
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get form data
    $category = $_POST['category'];
    $name = $_POST['name'];
    $description = $_POST['description'];

    // Handle image upload
    $image = "";
    if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
        $image = 'uploads/' . basename($_FILES['image']['name']);
        move_uploaded_file($_FILES['image']['tmp_name'], $image);
    }

    // Insert data into the database
    $sql = "INSERT INTO news (category, image, name, description) 
            VALUES ('$category', '$image', '$name', '$description')";

    if ($conn->query($sql) === TRUE) {
        echo "New news article added successfully!";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News Portal - Add News</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEJ3Qv1aKqlP6Y14uBzRjWkYkZ9Kwv6Wv+fAK8v7dqUw7kffPFLvA6vdoCbRm" crossorigin="anonymous">
</head>
<body>

<div class="container mt-5">
    <h2>Add News Article</h2>
    <form action="index.php" method="POST" enctype="multipart/form-data">
  <div class="mb-3">
    <label for="category" class="form-label">Category</label>
    <select class="form-control" id="category" name="category" required>
        <option value="" disabled selected>Select Category</option>
        <option value="Politics">Politics</option>
        <option value="Sports">Sports</option>
        <option value="Technology">Technology</option>
        <option value="Health">Health</option>
        <option value="Entertainment">Entertainment</option>
        <option value="Business">Business</option>
        <option value="Education">Education</option>
        <option value="Science">Science</option>
        <option value="Environment">Environment</option>
        <option value="Lifestyle">Lifestyle</option>
        <option value="Travel">Travel</option>
        <option value="Food">Food</option>
        <option value="Culture">Culture</option>
        <option value="Art">Art</option>
        <option value="Music">Music</option>
        <option value="Religion">Religion</option>
        <option value="Social Issues">Social Issues</option>
        <!-- Add more categories as needed -->
    </select>
</div>

        <div class="mb-3">
            <label for="name" class="form-label">News Name</label>
            <input type="text" class="form-control" id="name" name="name" required>
        </div>
        <div class="mb-3">
            <label for="description" class="form-label">Description</label>
            <textarea class="form-control" id="description" name="description" rows="4" required></textarea>
        </div>
        <div class="mb-3">
            <label for="image" class="form-label">Upload Image</label>
            <input type="file" class="form-control" id="image" name="image" accept="image/*">
        </div>
        <button type="submit" class="btn btn-primary">Submit News</button>
    </form>
</div>

<!-- Bootstrap JS (Optional) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pzjw8f+ua7Kw1TIq0HpT6c8Fq//pYH5TtGp0stR7HzP1r9DJzV7EdHgV6sdyhdjd" crossorigin="anonymous"></script>
</body>
</html>
