[英]PHP/MySQL - Need help in correcting a PHP warning?
I found this script on about.com which I'm trying to learn from on how to create a rating system but the script gives me a warning that I listed below. 我在about.com上找到了该脚本,该脚本旨在向我学习如何创建评分系统,但是该脚本给了我下面列出的警告。
I was wondering how can I fix this problem? 我想知道如何解决这个问题? And what part of the code do I need to change and where? 我需要更改代码的哪一部分,在哪里?
Here is the warning below. 以下是警告。
Warning: Division by zero on line 43
Here is the script below. 这是下面的脚本。
<?php
// Connects to your Database
mysql_connect("localhost", "root", "", "sitename") or die(mysql_error());
mysql_select_db("sitename") or die(mysql_error());
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{
//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}
//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month);
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
}
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM vote") or die(mysql_error());
//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
{
//This outputs the sites name
Echo "Name: " .$ratings['name']."<br>";
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
$current = $ratings[total] / $ratings[votes];
Echo "Current Rating: " . round($current, 1) . "<br>";
//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
Echo "Rank Me: ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">Vote 1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">Vote 2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">Vote 3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">Vote 4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">Vote 5</a><p>";
}
?>
You need to make sure you aren't dividing using a 0. If the values you get for total
and votes
from MySQL are 0, you should bypass the division and set a fixed value. 您需要确保不使用0进行除法。如果您获得的total
值和MySQL的votes
均为0,则应绕开除法并设置一个固定值。
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if($ratings['total'] > 0 && $ratings['votes'] > 0) {
$current = $ratings['total'] / $ratings['votes'];
}
else{
$current = 0;
}
PS PS
Note how I quoted the elements in the $ratings
array. 注意如何引用$ratings
数组中的元素。 You should always do that. 您应该始终这样做。
// This is INCORRECT. Causes error notices if you have error reporting on.
// and can have other consequences if you happen to use a `total` constant.
$ratings[total];
// It should be
$ratings['total']
The problem is here 问题在这里
$current = $ratings[total] / $ratings[votes];
If there are no votes, you are dividing a number by zero. 如果没有投票,则是将数字除以零。 And that is bad :) 那很糟糕:)
Add some verification that $ratings[votes] is set and it is not 0. 添加一些验证,以确保设置了$ ratings [votes]且该值不为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.