-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_message.php
More file actions
31 lines (26 loc) · 993 Bytes
/
Copy pathsend_message.php
File metadata and controls
31 lines (26 loc) · 993 Bytes
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
<?php
session_start();
require 'db.php';
if (!isset($_SESSION['logged_in']) || $_SESSION['Category'] != 0) {
echo json_encode(['status' => 'error', 'message' => 'Unauthorized access']);
exit();
}
if (isset($_POST['message']) && isset($_GET['farmer_id'])) {
$message = mysqli_real_escape_string($conn, $_POST['message']);
$buyer_id = $_SESSION['id'];
$farmer_id = (int)$_GET['farmer_id'];
if (empty($message)) {
echo json_encode(['status' => 'error', 'message' => 'Message cannot be empty']);
exit();
}
$sql = "INSERT INTO messages (sender_id, receiver_id, message, created_at)
VALUES ($buyer_id, $farmer_id, '$message', NOW())";
if (mysqli_query($conn, $sql)) {
echo json_encode(['status' => 'success']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to send message']);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid request']);
}
?>