<?php
// Database connection
include "connection.php";

// Read POST data
$content = isset($_POST['content']) ? $_POST['content'] : '';
$sender = isset($_POST['sender']) ? $_POST['sender'] : '';

// Validate input
if (empty($content) || empty($sender)) {
    echo json_encode(["status" => "error", "message" => "Content or sender cannot be empty"]);
    exit();
}

// Prepare and execute the SQL statement
$stmt = $conn->prepare("INSERT INTO messages (content, sender) VALUES (?, ?)");
if (!$stmt) {
    echo json_encode(["status" => "error", "message" => "Failed to prepare statement"]);
    exit();
}
$stmt->bind_param("ss", $content, $sender);

if ($stmt->execute()) {
    echo json_encode(["status" => "success", "message" => "Message saved successfully"]);
} else {
    echo json_encode(["status" => "error", "message" => "Failed to save message"]);
}

$stmt->close();
$conn->close();
?>
