我正在创建一个网站,人们可以使用编号为1-10的无线电盒对内容进行评级。选中时,单选框会将评级发送到数据库,并且工作正常。然后我想要进行平均计算以找到平均评级并输出结果。我已经尝试了很多方法但无济于事,包括使用数组。术语“总计”'是提交评分的人员总数和“评分”一词。是有史以来提交的所有无线电盒值的总和。我将非常感谢任何帮助。
<?php
$con2=mysqli_connect("myhost","user","password","place");
// Check connection
if (mysqli_connect_errno($con2))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$value=$_POST['rating'];
$id=$_POST['id'];
mysqli_query ($con2, "UPDATE table SET totalraters = totalraters + 1 WHERE id='$id' ");
mysqli_query ($con2, "UPDATE table SET ratingsum = ratingsum + '$value' WHERE id='$id' ");
//Works great up to here
$ratingsum = mysqli_query($con2, "SELECT ratingsum FROM table WHERE id='$id' ");
$totalraters = mysqli_query($con2, "SELECT totalraters FROM table WHERE id='$id' ");
IF ($totalraters != 0) {
$rating = (int)($ratingsum/$totalraters);
}
mysqli_query ($con2, "UPDATE table SET rating ='$rating' WHERE id='$id' ") or trigger_error(mysqli_error($con2));
mysqli_close($con2);
header('Location: back to same page ');
?>
此刻,在评分完成后,我将返回到原始页面,但当前评级始终为1,而不是正确的结果,尽管它输出正确数量的评分者。
答案 0 :(得分:0)
Yippee我终于修好了,你的帮助也很好hichris123 ....非常感谢!!这花了我几个小时,所以我发布它可能会帮助别人。
<?php
$con2=mysqli_connect("webhost.com","account","password","user");
// Check connection
if (mysqli_connect_errno($con2))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$value=$_POST['rating'];
$id=$_POST['id'];
mysqli_query ($con2, "UPDATE table SET totalraters = totalraters + 1 WHERE id='$id' ");
mysqli_query ($con2, "UPDATE table SET ratingsum = ratingsum + '$value' WHERE id='$id' ");
$query1 = mysqli_query($con2, "SELECT ratingsum FROM table WHERE id='$id' ");
$query2 = mysqli_query($con2, "SELECT totalraters FROM table WHERE id='$id' ");
$row = mysqli_fetch_array($query1);
$ratingsum = intval($row['ratingsum']);
$row2 = mysqli_fetch_array($query2);
$totalraters = intval($row2['totalraters']);
$rating = (intval($ratingsum/$totalraters));
mysqli_query ($con2, "UPDATE table SET rating = '$rating' WHERE id='$id' ");
include ('index/starting page.html');