过滤数据时出错

php

25 观看

1回复

1 作者的声誉

我是新来的,我面临过滤数据而不显示在页面上的问题。我遇到错误,请帮助我。对不起,英语不好。

错误:

注意:未定义的变量:/storage/ssd3/688/2645688/public_html/test.php中的search_result

警告:mysqli_fetch_array()期望参数1为mysqli_result,在/storage/ssd3/688/2645688/public_html/test.php中给定null

<?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">
	<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 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>

作者: Irfan Bashir 的来源 发布者: 2017 年 9 月 15 日

回应 1


0

1242 作者的声誉

您会收到错误消息,因为页面加载if(isset($_POST['submit']))为false 时将$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>
作者: Muhammed Imran Hussain 发布者: 2017 年 9 月 15 日
32x32