Php 零误差除法
我正在尝试实现一个投票系统,当用户单击其中一个超链接时,页面将刷新,数据库应使用数据库信息进行更新,我得到了一个除法为零的错误,由于包含的条件语句,这种错误不再存在,但是,当用户单击其中一个超链接时,投票无效,数据库也不会更新Php 零误差除法,php,Php,我正在尝试实现一个投票系统,当用户单击其中一个超链接时,页面将刷新,数据库应使用数据库信息进行更新,我得到了一个除法为零的错误,由于包含的条件语句,这种错误不再存在,但是,当用户单击其中一个超链接时,投票无效,数据库也不会更新 <?php // Connects to your Database mysql_connect("localhost", "root", "password") or die(mysql_error()); mysql_select_db("dbnam
<?php
// Connects to your Database
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("dbname") 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 reports SET total = total + $voted, votes = votes + 1 WHERE reportID = $id");
Echo "Your vote has been cast <p>";
}
}
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM reports WHERE reportID = $id") 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['reportName']."<br>";
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if(isset($ratings['votes']) && $ratings['votes'] != 0){
$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['reportID'].">Vote 1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings['reportID'].">Vote 2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings['reportID'].">Vote 3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings['reportID'].">Vote 4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings['reportID'].">Vote 5</a><p>";
}
?>
任何帮助都将不胜感激
谢谢在进行除法运算之前,请先检查
$ratings[投票]
的值
if(isset($ratings[votes]) && $ratings[votes] != 0){
$current = $ratings[total] / $ratings[votes];
Echo "Current Rating: " . round($current, 1) . "<br>";
}
if(isset($ratings[voces])&&$ratings[voces]!=0){
$current=$ratings[总票数]/$ratings[票数];
回声“电流额定值:。圆形($Current,1)。”
”;
}
$ratings[投票]
为0。找出原因。顺便说一句,你遗漏了'total'
和'voates'
的引号(PHP允许这是一个历史性的意外,而不是shoutier警告是我最讨厌的事情之一)。如果你不明白这是一个错误,试着在计算器上、纸上或任何地方计算42/0
。这是一个没有正确答案的等式。最好检查$rating['total']
是否有一个正确的值,这取决于这些值的来源。@IMSoP是的,我同意你的意见。在我的数据库中,该字段默认设置为零,这可能是导致错误的原因吗?@user3296793是的。但是没关系。您可以从PHP
end检查这一点。