PHP警告:在第19行和第20行的C:\ xampp \ htdocs \ poll \ add.php中除以零

问题描述 投票:-2回答:3

我创建了一个投票投票概念。我在这段代码中有错误。我怎样才能实现这一目标?

我得到了这个代码,它不起作用,因为它是一个投票概念,它除以100来计算投票。我在localhost中运行它没有除以100,它除以0.我怎么能解决这个错误?

警告:在第19行的C:\ xampp \ htdocs \ poll \ add.php中除以零

警告:在第20行的C:\ xampp \ htdocs \ poll \ add.php中除以零

<?php
$connect = @mysqli_connect('localhost','root','','poll');
$poll = $_POST['poll'];
if (isset($poll))
{
    $SQL="";
    if ($poll =="1")
    {
        $SQL = "UPDATE poll SET yes =yes+1 WHERE id = 1";
        }
        elseif ($poll =="0"){
            $SQL="UPDATE poll SET no =no + 1 WHERE id = 1";
        }
        $query = mysqli_query($connect,$SQL);
        if($query)
        {
            $retSQL = mysqli_query($connect, "SELECT * FROM poll");
            $resarr = mysqli_fetch_array($retSQL);
            $yes = 100 * round($resarr['yes'] / ($resarr['no'] + $resarr['yes']),2);
            $no = 100 * round($resarr['no'] / ($resarr['yes'] + $resarr['no']),2);
            echo 'YES: '.$yes.'%';
            echo 'NO: '.$no.'%';                       
        }else{
            echo 'Bad';
        }

    }
?>
php sql
3个回答
2
投票

当表达式$resarr['no'] + $resarr['yes']评估为0时,您会收到此警告。当两个值均为零时就是这种情况,因此还没有任何投票。如果$resarr['no'] + $resarr['yes']评估为0,请事先检查


0
投票

我建议在数据库中进行计算开始〜它没有经过测试btw但看起来应该可以工作(着名的最后一句话)

$connect = @mysqli_connect('localhost','root','','poll');

if (isset( $_POST['poll'] ) ){

    $poll = $_POST['poll'];

    switch( $poll ){
        case 1:$sql='update `poll` set `yes`=`yes`+1 where `id` = 1';break;
        case 0:$sql='update poll set `no`=`no` + 1 where `id` = 1';break;
    }
    $result = mysqli_query( $connect, $sql );
    if( $result ){

        $sql='select 
            round( ( `yes`/ (`no` + `yes`) ), 2 ) as `yes`,
            round( ( `no` / (`yes` + `no`) ),2 ) as `no`
        from poll';

        $result=mysqli_query( $connect, $sql );
        if( $result ){
            while( $rs=mysqli_fetch_assoc( $result ) ){
                echo "yes: {$rs['yes']}\nNo: {$rs['no']}"
            }
        } else{
            echo 'Bad foo';
        }
    }
}



mysql> describe poll;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| yes   | int(11) unsigned | NO   |     | NULL    |                |
| no    | int(11) unsigned | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


mysql> select * from poll;
+----+-----+----+
| id | yes | no |
+----+-----+----+
|  1 |  32 | 26 |
+----+-----+----+
1 row in set (0.00 sec)



mysql> select
        round( ( `yes`/ ( `no` + `yes` ) ), 2 ) as `yes`,
        round( ( `no` / ( `yes` + `no` ) ), 2 ) as `no`
        from poll;
+------+------+
| yes  | no   |
+------+------+
| 0.55 | 0.45 |
+------+------+
1 row in set (0.00 sec)

或者,使用格式化结果

mysql> select 
            concat( round( round( ( `yes`/ ( `no` + `yes` ) ), 2 ) * 100,0),'%') as `yes`,
            concat( round( round( ( `no` / ( `yes` + `no` ) ), 2 ) * 100,0),'%') as `no`
        from poll;
+------+------+
| yes  | no   |
+------+------+
| 55%  | 45%  |
+------+------+
1 row in set (0.00 sec)

-1
投票

我认为,在你的计算中你应该用括号()压缩来得到正确的计算

$yes = 100 * round(($resarr['yes'] / ($resarr['no'] + $resarr['yes'])),2);
$no = 100 * round(($resarr['no'] / ($resarr['yes'] + $resarr['no'])),2);

删除php警告使用error_reporting(E_ERROR | E_PARSE); 无论如何,你可以遵循这个link

© www.soinside.com 2019 - 2024. All rights reserved.