-5

我想用计数显示喜欢和不同的投票。我的代码在下面给出,但它显示警告: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
}
?>
4

3 回答 3

0

$total在除法运算中使用之前检查变量

例如if ($total != 0),然后进行除法运算

于 2012-05-31T05:52:59.303 回答
0

如果一个资源没有被赞成或反对,它将导致在那里被零除:

$up_per=($up_value*100)/$total;
于 2012-05-31T05:53:01.127 回答
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;
}
于 2012-05-31T05:54:05.663 回答