-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_notifications.php
More file actions
72 lines (57 loc) · 1.85 KB
/
fetch_notifications.php
File metadata and controls
72 lines (57 loc) · 1.85 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
// Disable warnings from appearing in output
error_reporting(0);
ini_set('display_errors', 0);
header('Content-Type: application/json');
// Define database credentials correctly
$host = "localhost";
$username = "root"; // default XAMPP username
$password = ""; // default XAMPP password
$database = "lost_found_db";
try {
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
throw new Exception('Connection failed: ' . $conn->connect_error);
}
// Get user email from POST request
$userEmail = isset($_POST['email']) ? $_POST['email'] : '';
if (empty($userEmail)) {
throw new Exception('Email is required');
}
// Prepare SQL query
$query = "SELECT item_name, claim_date, claim_time
FROM claim_reports
WHERE claim_email = ?
AND status = 'Claimed'
AND remark = 'Approved'
ORDER BY claim_time DESC, claim_date DESC";
$stmt = $conn->prepare($query);
if (!$stmt) {
throw new Exception('Prepare failed: ' . $conn->error);
}
$stmt->bind_param("s", $userEmail);
if (!$stmt->execute()) {
throw new Exception('Execute failed: ' . $stmt->error);
}
$result = $stmt->get_result();
$notifications = array();
while ($row = $result->fetch_assoc()) {
$notifications[] = array(
'item_name' => $row['item_name'],
'claim_date' => date('Y-m-d', strtotime($row['claim_date'])),
'claim_time' => date('H:i:s', strtotime($row['claim_time']))
);
}
echo json_encode($notifications);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
} finally {
if (isset($stmt)) {
$stmt->close();
}
if (isset($conn)) {
$conn->close();
}
}
?>