插入给定评级的评级和调用平均值


Insert rating and calling average of that given rating

我试图在数据库中插入评级,并从数据库中调用给定评级的平均值

<?PHP
$connection = mysqli_connect("localhost", "akdnaklnd", "lfnlfns","faknfns");
$game = $_POST['game'];
$post_rating = $_POST['rating'];
$find_data = mysqli_query( "SELECT * FROM rates WHERE game='$game'");
while($row = mysqli_fetch_assoc($find_data)){
    $id=$row['id'];
    $current_rating = $row['rating'];
    $current_hits=$row['hits'];
}
$new_hits = $current_hits + 1;
$update_hits= mysqli_query("UPDATE rates SET hits = '$new_hits' WHERE id='$id'");                                  
$pre_rating= $current_rating + $post_rating;
$new_rating = $pre_rating / $new_hits;
$update_rating = mysqli_query("UPDATE rates SET rating ='$new_rating' WHERE     id='$id'");
header("location : average.php");
?>

尝试通过从代码中删除所有MySQL内容并保留重定向代码(没有空间)来调试它。

header("location:average.php");

如果它工作,那么你的MySQL查询一定是错误的。尝试在执行每个MySQL查询操作后添加错误检查。

mysqli_query添加$connection参数:

$find_data = mysqli_query($connection, "SELECT * FROM rates WHERE game='$game'");

删除冒号前location后的空格:header("location: average.php");

首先,您不需要选择数据,您可以像这样直接编写更新查询。请确保您传递变量而不是字符串,以便更好地练习使用双引号和单引号。

最重要的传递连接变量。

$update_hits= mysqli_query($connection,"UPDATE rates SET hits = hits+1 WHERE id='".$id."'");

尝试echo $update_hits,如果它给出一个,则查询成功,否则有问题。

可以使用错误日志

    if (!$result) {
        $errorQuery = $update_hits;
        throw new Exception(mysqli_error($connection));
       $errorMessage =  mysqli_real_escape_string($connection,$e->getMessage());
       echo $errorMessage;
       exit();
    }
    else{
       echo "Success";
    }