PHP / MySQL - PHP uyarı düzeltmek yardıma ihtiyacınız?

2 Cevap php

Ben bir derecelendirme sistemi oluşturmak için nasıl öğrenmek için çalışıyorum ama senaryo bana aşağıda listelenen bir uyarı verir about.com bu komut dosyası bulundu.

Ben bu sorunu nasıl düzeltebilirim merak ediyorum? Ve kodun hangi parçası nerede değiştirmek gerekir ve var?

Aşağıda uyarıdır.

Warning: Division by zero on line 43

Aşağıda yazısıdır.

<?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 Cevap

Sen emin bir 0 kullanılarak bölünmesi değil yapmak gerekir. Eğer total ve votes MySQL için olsun değerler 0 iseniz, bölünme bypass ve sabit bir değere ayarlamanız gerekir .

//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.
Note how I quoted the elements in the $ratings array. 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']

Sorun burada

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

Hiç oy varsa, sıfır bir numara bölünmesi vardır. Ve bu kötü :)

$ Değerlendirme [oy] ayarlandığında bazı doğrulama ekleyin ve 0 değildir.