我正在创建一个搜索字段来搜索表中的id
、firstname
、lastname
和email
registration
但得到错误:
注意:未定义的变量:在 D:\Xampp\htdocs\Registration_system\dashboard.php 第 13 行中的 valuetosearch
注意:未定义的变量:在 D:\Xampp\htdocs\Registration_system\dashboard.php 第 14 行中的查询
警告:mysqli_query((:第 27 行的 D:\Xampp\htdocs\Registration_system\dashboard.php 中的空查询
警告:mysqli_fetch_array(( 期望参数 1 mysqli_result,布尔值在第 61 行的 D:\Xampp\htdocs\Registration_system\dashboard.php 中给出
<style type="text/css">
table, tr, th, td
{
border: 1px solid black;
}
</style>
<?php
if (isset($_POST['valuetosearch'])) {
$valuetosearch =$_POST['valuetosearch'];
$valuetosearch = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$valuetosearch."'";
$search_result= filtertable($query);
}
else
{
$query= "SELECT * FROM `registration`";
$search_result= filtertable($query);
}
function filtertable($query){
require_once'config.php';
$filter_result= mysqli_query($CONN, $query);
return $filter_result;
}
?>
<h1>Welcome on dashboard </h1>
<ul>
<li><a href="dashboard.php">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="dashboard.php">Profile</a></li>
<li><a href="login.php">Logout</a></li>
</ul>
<form action="dashboard.php" method="POST">
<input type="text" name="valuetosearch" placeholder="Search here...">
<input type="submit" name="search" value="submit"> <br><br>
<table>
<tr>
<th>id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
<th>Phone number</th>
</tr>
<?php while($row= mysqli_fetch_array($search_result)): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php endwhile; ?>
</table>
</form>
将if (isset($_POST['search'])) { ... }
替换为
if (isset($_POST['search'])) {
$query = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$_POST['valuetosearch']."'";
$search_result = filtertable($query);}
$valuetosearch
在定义它的同一行中使用两次(用于存储字符串并作为sql中的变量(
未定义$query
代码中有很多错误 第一个错误在于您的代码
if (isset($_POST['search'])) {
$valuetosearch = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$valuetosearch."'";
$search_result= filtertable($query);
}
应该是
if (isset($_POST['search'])) {
$query = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$valuetosearch."'";
$search_result= filtertable($query);
}