<?php
include "database/database.php"; // Include your database connection

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Fetch and sanitize inputs
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);

    // Validate inputs
    if (!empty($name) && !empty($email)) {
        // Prepare the SQL statement
        $stmt = $connection->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
        if ($stmt) {
            $stmt->bind_param("ss", $name, $email);

            // Execute and check if successful
            if ($stmt->execute()) {
                echo "<span style='color: green;'>Data inserted successfully!</span>";
            } else {
                echo "<span style='color: red;'>Error inserting data: " . $stmt->error . "</span>";
            }
            $stmt->close();
        } else {
            echo "<span style='color: red;'>Failed to prepare statement.</span>";
        }
    } else {
        echo "<span style='color: orange;'>Please fill in all fields.</span>";
    }
    $connection->close();
} else {
    echo "<span style='color: red;'>Invalid request method.</span>";
}
?>
