我已经建立了一个基于 jquery-php 的 5 星评级系统。
评级与命中数一起插入/存储在数据库中。
当重复点击任何星星时,在将评级插入数据库时会出现一些问题,即如果连续多次点击星星,评级不会被插入,但点击会被插入,这会影响新的评级结果。
这是一个 php 后端问题,我尝试仅在插入评分时插入命中,但它不起作用。
PHP
<?php
$post_rating = '5';
$id = '1';
$database = 'comment-database';
$tablename = 'comment-table';
require_once('rt-connect.php');
$find_data = "SELECT hits, user_rating, rating_total FROM $tablename WHERE id='$id'";
$query = mysqli_query($connection, $find_data) or trigger_error(mysqli_connect_error(), E_USER_ERROR);
$row = mysqli_fetch_assoc($query);
$current_hit = $row['hits'];
$current_rating_total = $row['rating_total'];
$new_hits = $current_hit+1;
$new_rating_total = $current_rating_total + $post_rating;
$new_rating = $new_rating_total / $new_hits;
$new_round_rating = ceil($new_rating/0.5)*0.5;
$update_rating = mysqli_query($connection, "UPDATE $tablename SET user_rating='$new_round_rating' WHERE id='$id'");
if ($update_rating){
$update_hits = mysqli_query($connection, "UPDATE $tablename SET hits='$new_hits' WHERE id='$id'");
$update_rating_total = mysqli_query($connection, "UPDATE $tablename SET rating_total='$new_rating_total' WHERE id='$id'");
}
$find_data2 = "SELECT hits, user_rating, raw_rating, rating_total FROM $tablename WHERE id='$id'";
$query2 = mysqli_query($connection, $find_data2);
$row2 = mysqli_fetch_assoc($query2);
$current_hit2 = $row2['hits'];
$current_rating2 = $row2['user_rating'];
$current_raw_rating_total2 = $row2['raw_rating'];
$current_rating_total2= $row2['rating_total'];
echo $current_hit2.'<br />';
echo $current_rating2.'<br />';
echo $current_raw_rating_total2.'<br />';
echo $current_rating_total2.'<br />';
?>
请查看并建议任何可能的方法。
谢谢。