-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubmitAppRating.php
54 lines (44 loc) · 1.65 KB
/
submitAppRating.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
52
53
54
<?php
include 'dbconnection.php';
// Establish database connection
$conn = dbconnection();
// Check connection
if ($conn->connect_error) {
die(json_encode(['status' => 'error', 'message' => 'Database connection failed.']));
}
// Get data from POST request
$uemail = isset($_POST['uemail']) ? trim($_POST['uemail']) : '';
$app_rating = isset($_POST['app_rating']) ? (int)$_POST['app_rating'] : 0;
// Debugging output
error_log("Received uemail: $uemail, app_rating: $app_rating");
$response = [];
// Validate input
if (!empty($uemail) && filter_var($uemail, FILTER_VALIDATE_EMAIL) && $app_rating >= 1 && $app_rating <= 5) {
// Check if the user has already rated
$stmt = $conn->prepare("SELECT * FROM app_rating_db WHERE uemail = ?");
$stmt->bind_param("s", $uemail);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$response['status'] = 'error';
$response['message'] = 'You have already rated the app.';
} else {
// Insert the new rating
$stmt = $conn->prepare("INSERT INTO app_rating_db (uemail, app_rating) VALUES (?, ?)");
$stmt->bind_param("si", $uemail, $app_rating);
if ($stmt->execute()) {
$response['status'] = 'success';
$response['message'] = 'Rating submitted successfully.';
} else {
$response['status'] = 'error';
$response['message'] = 'Error submitting rating. Please try again later.';
}
}
$stmt->close();
} else {
$response['status'] = 'error';
$response['message'] = 'Invalid input. Please provide a valid rating.';
}
$conn->close();
echo json_encode($response);
?>