2009-12-11 4 views
0

등급 시스템을 만드는 방법에 대해 배우려는 about.com에서이 스크립트를 발견했지만 아래에 나와있는 스크립트에서 스크립트가 표시됩니다.PHP/MySQL - PHP 경고 수정에 도움이 필요하십니까?

이 문제를 어떻게 해결할 수 있습니까? 그리고 코드의 어느 부분을 변경해야합니까?

아래 경고가 있습니다.

Warning: Division by zero on line 43 

아래 스크립트는 다음과 같습니다.

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

답변

2

당신은 당신이 MySQL의에서 totalvotes 얻을 값이 0 인 경우 A는 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; 
} 

P.S.
$ratings 배열의 요소를 인용 한 것에 유의하십시오. 항상 그렇게해야합니다.

// 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'] 
+0

+1 좋은 캐치하지 않습니다 – Vafliik

2

문제는 전혀 표가없는 경우, 당신은 0으로 숫자를 나누어 여기에

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

입니다.) 그리고 그 :)

이 설정되어 $ 등급 [표] 몇 가지 검증을 추가 나쁜이며 따옴표와 0

관련 문제