2012-05-31 27 views
-5

我想顯示與count和count不同的顯示。我的代碼在下面給出,但它顯示警告:在filename.php中除零。通過零警告進行故障排除劃分

<?php 
include("db.php"); 

if($_POST['id']) 
{ 
$id=mysql_escape_String($_POST['id']); 
$name=mysql_escape_String($_POST['name']); 

mysql_query("update messages set $name=$name+1 where id='$id'"); 

$result=mysql_query("select up,down from messages where id='$id'"); 
$row=mysql_fetch_array($result); 

$up_value=$row['up']; 
$down_value=$row['down']; 
$total=$up_value+$down_value; 

$up_per=($up_value*100)/$total; 
$down_per=($down_value*100)/$total; 
?> 
<div style="margin-bottom:10px"> 
<b>Ratings for this blog</b> (<?php echo $total; ?> total) 
</div> 
<table width="700px"> 

<tr> 
<td width="30px"></td> 
<td width="60px"><?php echo $up_value; ?></td> 
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td> 
</tr> 

<tr> 
<td width="30px"></td> 
<td width="60px"><?php echo $down_value; ?></td> 
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td> 
</tr> 

</table> 

<?php 
} 
?> 
+5

「這樣做對我」的問題被皺起眉頭,你應該顯示http://WhatHaveYouTried.com並問路。在主題上,你唯一的分區是'/ $ total',所以'$ total'等於'0'很明顯。你必須找出爲什麼會發生這種行爲,並防止它被0除。簡單如此。 –

+2

你認爲$總數可能爲零嗎? –

回答

0

檢查變量$total你除法運算

例如使用前if ($total != 0)然後做除法調度研究

0

如果資源一直沒有起來,投票或向下投票,這將導致被零除有:

$up_per=($up_value*100)/$total; 
0
if ($total !== 0) { 
    $up_per = ($up_value*100)/$total; 
    $down_per = ($down_value*100)/$total; 
} else { 
    // what do you want to show when there is no votes. 
    $up_per = 0; 
    $down_per = 0; 
}