2017-01-11 72 views
0

在本網站發佈我的第一個問題。如何在搜索前隱藏php中SQL表格的顯示?

我做了一個php網頁,其中的文本框將接受搜索關鍵字,當提交時,該表的行將被顯示。簡單!

現在,在搜索之前,整個表格已經顯示在我的php頁面上了。我不想那樣。我只想搜索後的結果,在搜索之前不應該顯示錶(測試數據表有500行,但實際的表有15,000行)。

頁(前搜索):

<?php 
      if(isset($_REQUEST['submit'])){ 
    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $sql=" SELECT * FROM live_table WHERE name like '%".$name."%' AND company LIKE '%".$email."%'"; 
    $q=mysqli_query($con, $sql); 
} 
else{ 
    $sql="SELECT * FROM live_table"; 
    $q=mysqli_query($con, $sql); 
} 
?> 
<form method="post" class="search"> 

    <table width="200"> 
    <tr> 
    <td></td> 
    <td><input class="form__input" type="search" autocomplete="off" name="name" placeholder="Name" value="<?php if(isset($name)) echo $name;?>" /></td> 
    <td></td> 
    <td><input class="form__input" autocomplete="off" type="search" name="email" placeholder="Company Name" value="<?php if(isset($email)) echo $email;?>" /></td> 
    <td><input type="submit" name="submit" value=" Find " class="button"/></td> 
    </tr> 
</table> 
</form> 



<table width="70%" cellpadding="5" cellspace="5" style="margin: 3em 3em 3em 3em;"> 

      <tr> 
       <td><strong>Salutation</strong></td> 
       <td><strong>First Name</strong></td> 
       <td><strong>Middle Name</strong></td> 
       <td><strong>Last Name</strong></td> 


      </tr> 

<?php 
    while($res=mysqli_fetch_array($q)){ 
    ?> 
      <tr> 
       <td><?php echo $res['id'];?></td> 
       <td><?php echo $res['name'];?></td> 
       <td><?php echo $res['company'];?></td> 
       <td><?php echo $res['zip'];?></td> 
       <td><?php echo $res['city'];?></td> 
      </tr> 

      <?php } ?> 
      </table> 

回答

-1
<?php 
$dont_show = false; 

      if(isset($_REQUEST['submit'])){ 
    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $sql=" SELECT * FROM live_table WHERE name like '%".$name."%' AND company LIKE '%".$email."%'"; 
    $q=mysqli_query($con, $sql); 
} 
else{ 
    $dont_show = true; 
} 
?> 
<form method="post" class="search"> 

    <table width="200"> 
    <tr> 
    <td></td> 
    <td><input class="form__input" type="search" autocomplete="off" name="name" placeholder="Name" value="<?php if(isset($name)) echo $name;?>" /></td> 
    <td></td> 
    <td><input class="form__input" autocomplete="off" type="search" name="email" placeholder="Company Name" value="<?php if(isset($email)) echo $email;?>" /></td> 
    <td><input type="submit" name="submit" value=" Find " class="button"/></td> 
    </tr> 
</table> 
</form> 


<?php if(!$dont_show){ ?> 
<table width="70%" cellpadding="5" cellspace="5" style="margin: 3em 3em 3em 3em;"> 

      <tr> 
       <td><strong>Salutation</strong></td> 
       <td><strong>First Name</strong></td> 
       <td><strong>Middle Name</strong></td> 
       <td><strong>Last Name</strong></td> 


      </tr> 

<?php 
    while($res=mysqli_fetch_array($q)){ 
    ?> 
      <tr> 
       <td><?php echo $res['id'];?></td> 
       <td><?php echo $res['name'];?></td> 
       <td><?php echo $res['company'];?></td> 
       <td><?php echo $res['zip'];?></td> 
       <td><?php echo $res['city'];?></td> 
      </tr> 

      <?php } ?> 
      </table> 
<?php } ?> 
+0

說明:未定義變量:●在[文件路徑] 警告:mysqli_fetch_array()第77行是-------> while($ res = mysqli_fetch_array($ q)){ – user7403740

+0

coz $ q aint visible goddamit just copy(參數1是mysqli_result,null在[文件路徑]第77行中給出的)粘貼上面的代碼 –

+0

或者你可以檢查'$ q'是否存在 –

0

首先,你的代碼是針對SQL注入脆弱的:從來沒有在一個SQL查詢使用原始用戶輸入(或任何地方else)請參閱How can I prevent SQL injection in PHP?

您需要刪除else塊中的查詢

else{ 
    $sql="SELECT * FROM live_table"; 
    $q=mysqli_query($con, $sql); 
} 

然後,打印表格的行之前,添加一個測試,以檢查是否$q定義

<?php if (isset($q)) : ?> 
    <?php while($res=mysqli_fetch_array($q)): ?> 
     <tr> 
      <td><?php echo $res['id'];?></td> 
      <td><?php echo $res['name'];?></td> 
      <td><?php echo $res['company'];?></td> 
      <td><?php echo $res['zip'];?></td> 
      <td><?php echo $res['city'];?></td> 
     </tr> 

    <?php endwhile; ?> 
<?php endif; ?> 
相關問題