我是新来的,我面临着过滤数据而不在页面上显示的问题。 我收到错误请帮助我。抱歉英语不好。
错误:
注意:未定义的变量:search_result in /storage/ssd3/688/2645688/public_html/test.php在线
警告: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>
答案 0 :(得分:0)
您收到错误是因为当网页加载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>