-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_status.php
More file actions
54 lines (42 loc) · 1.57 KB
/
update_status.php
File metadata and controls
54 lines (42 loc) · 1.57 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
<?php
header('Content-Type: application/json');
include 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['status']) || !isset($data['response_id'])) {
echo json_encode(['success' => false, 'message' => 'Missing data']);
exit;
}
$status = $data['status'];
$response_id = $data['response_id'];
$time_column = $status . '_time';
$current_time = date('Y-m-d H:i:s');
// Validate response_id exists
$check_sql = "SELECT id FROM emergency_responses WHERE id = ?";
$check_stmt = mysqli_prepare($conn, $check_sql);
mysqli_stmt_bind_param($check_stmt, "i", $response_id);
mysqli_stmt_execute($check_stmt);
mysqli_stmt_store_result($check_stmt);
if (mysqli_stmt_num_rows($check_stmt) == 0) {
echo json_encode(['success' => false, 'message' => 'Invalid response ID']);
mysqli_stmt_close($check_stmt);
exit;
}
mysqli_stmt_close($check_stmt);
try {
// Update emergency_responses
$sql = "UPDATE emergency_responses SET $time_column = ? WHERE id = ?";
$stmt = mysqli_prepare($conn, $sql);
if (!$stmt) {
throw new Exception("Prepare failed: " . mysqli_error($conn));
}
mysqli_stmt_bind_param($stmt, "si", $current_time, $response_id);
if (!mysqli_stmt_execute($stmt)) {
throw new Exception("Execute failed: " . mysqli_stmt_error($stmt));
}
mysqli_stmt_close($stmt);
echo json_encode(['success' => true, 'time' => $current_time]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
} finally {
mysqli_close($conn);
}