-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequesthelp.php
51 lines (42 loc) · 1.92 KB
/
Requesthelp.php
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
<?php
include "Conx.php";
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = json_decode(file_get_contents("php://input"), true);
if (isset($data['answer'], $data['help_id'], $data['user_id'])) {
$answer = $data['answer'];
$help_id = $data['help_id'];
$user_id = $data['user_id'];
$stmt = $conn->prepare("INSERT INTO answers (answer, help_id, user_id) VALUES (?, ?, ?)");
$stmt->bind_param("sii", $answer, $help_id, $user_id);
if ($stmt->execute()) {
$updateStmt = $conn->prepare("UPDATE help_requests SET is_resolved = 1 WHERE id = ?");
$updateStmt->bind_param("i", $help_id);
$updateStmt->execute();
$updateStmt->close();
echo json_encode(['success' => true, 'message' => 'Solution submitted successfully']);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to submit solution']);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => 'Missing required data']);
}
}
// Handle GET requests
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$stmt = $conn->prepare("SELECT hr.id, hr.crop, hr.irrigation, IFNULL(hr.plastic_house, 'no') AS plastichouse, hr.problem, u.name AS user_name
FROM help_requests hr
LEFT JOIN users u ON hr.user_id = u.id
WHERE hr.is_resolved = 0");
$stmt->execute();
$result = $stmt->get_result();
$requests = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode(['success' => true, 'data' => $requests]);
$stmt->close();
}
$conn->close();
?>