-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratemovie.php
37 lines (29 loc) · 1.21 KB
/
ratemovie.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
<?php
include 'db.php';
session_start();
$userId = $_GET["userId"];
$movieId = $_GET["movieId"];
$userrating = $_GET["rating"];
$sql = "INSERT INTO movie_rating (user_id, movie_id, rating) VALUES ($userId, $movieId, $userrating)";
if(!mysqli_query($conn, $sql)) {
echo "<p>ERROR: Failed to execute query $sql ".mysqli_error($conn)."</p>";
}
else {
//Extracting average movie rating from the table movie_rating
$sql = "SELECT AVG(rating) AS avg, COUNT(rating) AS votes FROM movie_rating WHERE movie_id=$movieId ";
if($result = mysqli_query($conn, $sql)) {
$row = mysqli_fetch_assoc($result);
$rating = round($row['avg']);
$number_of_votes = $row['votes'];
}
else {
echo "<p>ERROR: Failed to execute query $sql ".mysqli_error($conn)."</p>";
}
//Inserting the computed average movie rating into the table movie
$sql = "UPDATE movie SET rating=$rating WHERE movie_id=$movieId";
if(!mysqli_query($conn, $sql)) {
echo "<p>ERROR: Failed to execute query $sql ".mysqli_error($conn)."</p>";
}
echo "<p><span id='heart'>♥</span> <span id='value'> $rating% </span> ($number_of_votes votes)</p><div id='slider-container'><p>Your rating: <span id='userrating'> $userrating%</span></p>";
}
?>