-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_user_message.php
More file actions
42 lines (35 loc) · 1.36 KB
/
send_user_message.php
File metadata and controls
42 lines (35 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
header('Content-Type: application/json');
include 'db.php';
function logDebug($message, $data = null) {
error_log($message . ($data ? ": " . print_r($data, true) : ""));
}
if (!isset($_POST['message']) || !isset($_POST['sender_id'])) {
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
exit;
}
$message = $_POST['message'];
$sender_id = $_POST['sender_id'];
$is_admin = 0; // Always 0 for user messages
try {
// Insert message from user
$stmt = $conn->prepare("INSERT INTO messages (sender_id, message, is_admin) VALUES (:sender_id, :message, :is_admin)");
$stmt->bindParam(':sender_id', $sender_id);
$stmt->bindParam(':message', $message);
$stmt->bindParam(':is_admin', $is_admin);
if ($stmt->execute()) {
$lastId = $conn->lastInsertId();
logDebug("User message inserted successfully", [
'message_id' => $lastId,
'sender_id' => $sender_id
]);
echo json_encode(['status' => 'success']);
} else {
logDebug("Failed to insert message", $stmt->errorInfo());
echo json_encode(['status' => 'error', 'message' => 'Failed to send message']);
}
} catch (PDOException $e) {
logDebug("Database error", $e->getMessage());
echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
}
?>