-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate_recipe.php
67 lines (60 loc) · 2.63 KB
/
rate_recipe.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
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
session_start();
require 'server.php';
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
echo json_encode(['error' => 'User not logged in']);
exit();
}
// Proceed if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_id = $_SESSION['user_id']; // Use session to get the logged-in user ID
$recipe_id = (int) $_POST['recipe_id'];
$taste_rating = (int) $_POST['taste_rating'];
// Validate rating input (assuming rating is between 1 and 5)
if ($taste_rating < 1 || $taste_rating > 5) {
echo json_encode(['error' => 'Invalid rating value']);
exit();
}
// Check if the user already rated the recipe
$check_query = "SELECT * FROM ratings WHERE user_id = ? AND recipe_id = ?";
$stmt = mysqli_prepare($conn, $check_query);
mysqli_stmt_bind_param($stmt, "ii", $user_id, $recipe_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) > 0) {
// Update the existing rating
$update_query = "UPDATE ratings SET taste_rating = ? WHERE user_id = ? AND recipe_id = ?";
$stmt_update = mysqli_prepare($conn, $update_query);
if ($stmt_update === false) {
echo json_encode(['error' => 'Database error during rating update']);
exit();
}
mysqli_stmt_bind_param($stmt_update, "iii", $taste_rating, $user_id, $recipe_id);
mysqli_stmt_execute($stmt_update);
} else {
// Insert a new rating
$insert_query = "INSERT INTO ratings (user_id, recipe_id, taste_rating) VALUES (?, ?, ?)";
$stmt_insert = mysqli_prepare($conn, $insert_query);
if ($stmt_insert === false) {
echo json_encode(['error' => 'Database error during rating insert']);
exit();
}
mysqli_stmt_bind_param($stmt_insert, "iii", $user_id, $recipe_id, $taste_rating);
mysqli_stmt_execute($stmt_insert);
}
// Calculate the updated average taste rating for the recipe
$avg_query = "SELECT AVG(taste_rating) as avg_taste_rating FROM ratings WHERE recipe_id = ?";
$stmt_avg = mysqli_prepare($conn, $avg_query);
if ($stmt_avg === false) {
echo json_encode(['error' => 'Database error during average rating calculation']);
exit();
}
mysqli_stmt_bind_param($stmt_avg, "i", $recipe_id);
mysqli_stmt_execute($stmt_avg);
$result_avg = mysqli_stmt_get_result($stmt_avg);
$avg_rating = mysqli_fetch_assoc($result_avg)['avg_taste_rating'];
// Return the updated average rating as a JSON response
echo json_encode(['avg_rating' => round($avg_rating, 1)]);
}
?>