2012-05-31 29 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; 
}