?php $conn = mysqli_connect('localhost:3306', 'ratingSystem', 'ratingSystem', 'elaborup_ratingSystem'); if (isset($_POST['save'])) { $uID = $conn->real_escape_string($_POST['uID']); $ratedIndex = $conn->real_escape_string($_POST['ratedIndex']); $ratedIndex++; if (!$uID) { $conn->query("INSERT INTO stars (rateIndex) VALUES ('$ratedIndex')"); $sql = $conn->query("SELECT id FROM stars ORDER BY id DESC LIMIT 1"); $uData = $sql->fetch_assoc(); $uID = $uData['id']; } else $conn->query("UPDATE stars SET rateIndex='$ratedIndex' WHERE id='$uID'"); exit(json_encode(array('id' => $uID))); } //numero persone che hanno recensito $sql = $conn->query("SELECT id FROM stars"); $numR = $sql->num_rows; //numero di persone che hanno dato il TOP $sql= ("SELECT COUNT(*) FROM stars WHERE rateIndex=5"); $result = mysqli_query($conn, $sql); $row = mysqli_fetch_array($result); //media dei voti $sql = $conn->query("SELECT SUM(rateIndex) AS total FROM stars"); $rData = $sql->fetch_array(); $total = $rData['total']; $avg = $total / $numR; ?> Rating System