2017-12-20 21 views
-2

投票投票の概念を作成しました。私はこのコードに誤りがあります。どうすればそれを達成できますか?PHP警告:19行目と20行目のC: xampp htdocs poll add.phpで0除算

私はこのコードを手に入れました。これは投票コンセプトであるため動作しません。投票を計算するために100で割ったものです。私は100で割っていないlocalhostで実行し、0で割った。どのように私はこのエラーを解決できますか? \ xamppの\ htdocsにライン上のポーリングの\ add.php 19

警告\::ゼロによる除算はC:\ xamppの\ htdocsにポーリングの\ add.php \上

警告:Cでのゼロによる除算ライン20

<?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'; 
     } 

    } 
?> 

答えて

2

表現$resarr['no'] + $resarr['yes']0と評価されたときあなたは、この警告を取得します。これは、両方の値がゼロの場合のため、まだ投票がありません。 $resarr['no'] + $resarr['yes']は私が考える0

-1

に評価された場合、あなたの計算に使用すると、PHPの警告を削除するには、正しい計算

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

を思い付く括弧()で圧縮する必要があり、事前に確認してくださいあなたはこのlink

に従うことができますとにかく error_reporting(E_ERROR | E_PARSE);
を使用
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) 
+0

コードは、コードの –

+0

どの部分を動作していませんか? – RamRaider

関連する問題