2017-09-15 65 views
0

我是新來的,在這裏我面臨着過濾數據而不顯示在頁面上的問題。 我收到錯誤,請幫助我。對不起英文不好。獲取數據過濾錯誤

錯誤:

Notice: Undefined variable: search_result in /storage/ssd3/688/2645688/public_html/test.php on line

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /storage/ssd3/688/2645688/public_html/test.php on line

<?php 
 
if(isset($_POST['submit'])) 
 
{ 
 
    $valueToSearch = $_POST['search']; 
 

 
$query = "SELECT * FROM `login` WHERE CONCAT(`id`, `username`, `password`) LIKE '%".$valueToSearch."%'"; 
 
    $search_result = filterTable($query); 
 
    
 
} 
 

 
function filterTable($query) 
 
{ 
 

 
$connect= mysqli_connect("localhost","root","","cable"); 
 
$filter_Result = mysqli_query($connect, $query); 
 
    return $filter_Result; 
 
} 
 

 
?> 
 
<form action="test.php" method="POST"> 
 
<div class="container"> 
 
\t <div class="row"> 
 
     <br><br><div class="search"> 
 
<input type="text" name="search" class="form-control input-sm" maxlength="64" placeholder="Search" /> 
 
<button type="submit" name="submit" class="btn btn-primary btn-sm">Search</button> 
 
</div> 
 
\t </div> 
 
</div> 
 
</br></br> 
 
</form> 
 

 
<table> 
 
       <tr> 
 
        <th>Id<br></th> 
 
        <th>Username:</th> 
 
        <th>Password</th> 
 
        </tr> 
 
              <?php while($row = mysqli_fetch_array($search_result)):?> 
 

 
       <tr> 
 
        <td><?php echo $row['id'];?><br></td> 
 
        <td><?php echo $row['username'];?></td> 
 
        <td><?php echo $row['password'];?></td> 
 
        
 
       </tr> 
 
       <?php endwhile;?> 
 
      </table>

+1

把函數的頂部。請在filterTable($ connect,$ query)中添加$ connect。 –

回答

0

你得到的錯誤,因爲當頁面加載if(isset($_POST['submit']))是假的,因此$search_result = filterTable($query);不執行,因此mysqli_fetch_array得到NULL

解決方法很簡單,我們要記住這一點,在頁面加載可能沒有$search_result

<?php 
$search_result = NULL; 

if(isset($_POST['submit'])) 
{ 
    $valueToSearch = $_POST['search']; 
    $query = "SELECT * FROM `login` WHERE CONCAT(`id`, `username`, `password`) LIKE '%".$valueToSearch."%'"; 
    $search_result = filterTable($query); 
} 

function filterTable($query) 
{ 
    $connect= mysqli_connect("localhost","root","","cable"); 
    $filter_Result = mysqli_query($connect, $query); 
    return $filter_Result; 
} 

?> 
<form action="test.php" method="POST"> 
<div class="container"> 
    <div class="row"> 
     <br><br><div class="search"> 
<input type="text" name="search" class="form-control input-sm" maxlength="64" placeholder="Search" /> 
<button type="submit" name="submit" class="btn btn-primary btn-sm">Search</button> 
</div> 
    </div> 
</div> 
</br></br> 
</form> 

<table> 
       <tr> 
        <th>Id<br></th> 
        <th>Username:</th> 
        <th>Password</th> 
        </tr> 
              <?php if(!empty($search_result)): while($row = mysqli_fetch_array($search_result)):?> 

       <tr> 
        <td><?php echo $row['id'];?><br></td> 
        <td><?php echo $row['username'];?></td> 
        <td><?php echo $row['password'];?></td> 

       </tr> 
       <?php endwhile; endif; ?> 
      </table> 
+0

這個人是對的。當你加載頁面時,它會給你錯誤。$ search_result將會變爲NULL/undefined。 –

+0

非常感謝的人。 –

+0

@IfanfanBashir請注意或接受答案,如果有幫助 –