-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_message_to_admin.php
More file actions
47 lines (41 loc) · 1.33 KB
/
send_message_to_admin.php
File metadata and controls
47 lines (41 loc) · 1.33 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
45
46
47
<?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') {
$sender_id = $_POST['sender_id'];
$message = $_POST['message'];
$created_at = $_POST['created_at'] ?? date('Y-m-d H:i:s');
// Prepare the insert statement for the admin_message table
$stmt = $conn->prepare("INSERT INTO admin_message (sender_id, receiver_id, user_id, message, created_at) VALUES (?, ?, ?, ?, ?)");
// Fetch all admin IDs from the admins table
$admin_ids = array();
$admin_query = "SELECT id FROM admins";
$result = $conn->query($admin_query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$admin_ids[] = $row['id'];
}
}
$success = true;
foreach ($admin_ids as $admin_id) {
$stmt->bind_param("iiiss", $sender_id, $admin_id, $sender_id, $message, $created_at);
if (!$stmt->execute()) {
$success = false;
break;
}
}
$response['success'] = $success;
} else {
$response['success'] = false;
}
echo json_encode($response);
$stmt->close();
$conn->close();
?>