2013-12-07 44 views
-1

我正在尝试在我的网站上创建评分系统,但是当我输入以下代码时,它会给我发出警告,“警告:在/ home/*/public_html/* *在线38" 上警告:按零消息划分

<?php 
// Connects to your Database 
    include ('config.php'); 

    //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>"; 
} 
?> 

PS 如何将使用cookie的支票更改为mysql数据库。我希望它检查数据库以查看该人是否刚刚评级。

+0

你不能简单地通过mysql存储改变cookie。您需要某种方式将用户与他们的db记录关联起来(例如登录系统)。 –

回答

1

在线38上,您有$ratings[total]/$ratings[votes]。这里的问题是,如果rating数组中的投票数量为0,那么您除以零。

$current = $ratings['votes'] > 0 ? $ratings['total']/$ratings['votes'] : 0;

+0

谢谢@Navarr – user3078799

+0

如果你喜欢它,@ user3078799,你应该将它标记为答案。代码中还有一些其他问题,包括潜在的SQL注入和其他安全问题。如果你关心这一点,并且正在寻找该领域的任何职业,我建议阅读[PHP The Right Way](http://www.phptherightway.com/)。 – Navarr

3

第38行:

$current = $ratings[total]/$ratings[votes];

也许应该

$current = $ratings['total']/$ratings['votes'];

何况在代码中的其他问题。