-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_user_messages.php
More file actions
44 lines (34 loc) · 1.06 KB
/
get_user_messages.php
File metadata and controls
44 lines (34 loc) · 1.06 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
43
44
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lost_found_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$response = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$user_id = $_POST['user_id'];
// Fetch messages sent by admins to the user and messages sent by the user to admins
$query = "SELECT DISTINCT sender_id, message, created_at
FROM admin_message
WHERE user_id = ? OR sender_id = ?
ORDER BY created_at ASC";
$stmt = $conn->prepare($query);
$stmt->bind_param("ii", $user_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
$messages = array();
while ($row = $result->fetch_assoc()) {
$messages[] = $row;
}
$response['success'] = true;
$response['messages'] = $messages;
} else {
$response['success'] = false;
$response['error'] = "Invalid request method.";
}
echo json_encode($response);
$conn->close();
?>