<?php
// Database configuration
$host = "localhost";      // Database host (keep this as localhost)
$user = "root";           // Database username (default is root for WAMP)
$password = "";           // Database password (leave empty for WAMP)
$dbname = "art_db"; // Database name (change if your DB has a different name)

// Create connection
$conn = new mysqli($host, $user, $password, $dbname);

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

// Optional: set character encoding
$conn->set_charset("utf8");

// Now $conn is ready to be used in other PHP files


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get data from form
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $message = trim($_POST['message']);

    // Validate inputs
    if (!empty($name) && !empty($email) && !empty($message)) {
        // Prepare SQL
        $stmt = $conn->prepare("INSERT INTO messages (name, email, message) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $name, $email, $message);

        if ($stmt->execute()) {
            echo "<script>alert('Message Sent Successfully!'); window.location='Home.php';</script>";
        } else {
            echo "<script>alert('Error saving message! Please try again.'); window.history.back();</script>";
        }

        $stmt->close();
    } else {
        echo "<script>alert('Please fill in all fields.'); window.history.back();</script>";
    }
}
$conn->close();
?>
