如何在选择不同的查询上应用条件

时间:2015-07-16 07:18:30

标签: php html random mysqli

我在php中创建一个网页,在0到99之间在屏幕上创建随机数。然后将其存储在数据库中,然后通过选择不同的查询来获取它,因为我不想重复这些数字..问题是我当没有选择明确的号码时,想要回显某些东西或显示警告框。我无法弄清楚..请有人帮助我..

<?php
$con=mysqli_connect('localhost','root','','bingo');
   if(isset($_POST['btn1'])){
        $sqli_del="DELETE FROM `numbers`";
       $que=mysqli_query($con, $sqli_del);
      if($que){
      echo "ALL NUMBERS ARE DELETED!!!!";
      }
     else{
      echo 'There is somethin wrong with the query...';
     }

     }
   if(isset($_POST['btn'])){
  $var=mt_rand(0,99);

    $sql="INSERT INTO `numbers`(`number`) VALUES ('$var')";
     $query=mysqli_query($con, $sql);

    $sqli="SELECT DISTINCT number FROM `numbers`";
     $res=mysqli_query($con, $sqli);


     }


    ?>

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>Bingo</title>
      <style>
     .main{margin:0 auto;}
     .form{  margin: auto;
       width: 71%;}
    .form input{width:90px;
      height:50px;}
         .spin{background-color:green; color:white; border-radius:10px;font-
       weight:bold;}
     .del{background-color:red; color:white; border-radius:10px;font- 
    weight:bold;}
       .number{ 
    width: 110px;
       height: 107px;
      font-size: 98px;
       color: blue;
        background-color: black;
       padding-left: 22px;
   margin-left: 25px;}
   .left{float:left;}
   .right{float:left;padding:250px;}
  a{text-decoration:none;}
    .back{text-align:left !important;float:left;color:blue;}
     .button{float:left;padding-top:20px;}
     </style>
   </head>

  <body>
    <div class="main">
     <div class="form">
    <form method="POST" action="">
    <div class="left">
    <?php if(!empty($res)) {?>
     <div class="back">
    <?php
      $count=mysqli_num_rows($res);
     echo "(".$count.' '. 'numbers are printed..'.")"."<br>";

        while($row=mysqli_fetch_assoc($res)){
     $num=$row['number'];

      echo $num."<br>";}


     ?>
    </div>
    <?php } ?>
    </div>

     <div class="right">
     <?php if(!empty($num)) {?>

     <div class="number"> 
     <?php 
       echo $num ."<br>";?>
       </div>
     <?php } ?>
         <div class="button">
      <a href=""><input class="spin" type="submit" name="btn" value="Spin">           
      </a>
     <a href=""><input class="del" type="submit" name="btn1" value="Delete         
    All"></a>
     </div>
      </div>
    </form>
    </div>

    </div>
  </body>
    </html>

1 个答案:

答案 0 :(得分:-2)

根据您的代码

    <?php if(!empty($res)) {?>
       <div class="back">
       <?php
       $count=mysqli_num_rows($res);
         echo "(".$count.' '. 'numbers are printed..'.")"."<br>";
         while($row=mysqli_fetch_assoc($res)){
           $num=$row['number'];
           echo $num."<br>";
         }
    ?>
    </div>
    <?php
    } else {//empty, $res is false
        if(isset($_POST['btn'])){ //check whether post request
           echo "<span>no number</span>";
        }
    }

mysqli query

if ( false === $res) {

} else {

}