我在 phpmyadmin 中创建了一个数据库,插入的列和数据很少。现在我可以在浏览器中显示带有记录的表格。但我想要的是通过从数据库中过滤来显示特定值。以下是我编写的代码段。此时,如果我在搜索字段中输入任何值,它不会进行排序,也不会执行任何操作。
<?php
if(isset ($_POST['search']))
{
$valueToSearch=$_POST['valueToSearch'];
//$query="SELECT * FROM `books` WHERE CONCAT (`ISBN`, `Title`, `Author`)LIKE '%".$valueToSearch."%'";
$query="SELECT * FROM books WHERE CONCAT ('ISBN', 'Title', 'Author')LIKE '%".$valueToSearch."%'";
$serach_result=filterTable($query);
}else{
$query="SELECT * FROM books";
$serach_result=filterTable($query);
}
function filterTable($query){
$connect=mysqli_connect ("localhost", "root", "", "bookstore-new");
$filter_result=mysqli_query($connect, $query);
return $filter_result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Lab 4</title>
<style>
table, tr, th, td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="bookstorewebdev.php" method="post">
<input type="text" name="valueToSearch" placeholder="value to search" >
<br><br>
<input type="submit" name="Search" value="Filter" ><br><br>
<table>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
</tr>
<?php while ($row=mysqli_fetch_array($serach_result)):?>
<tr>
<td> <?php echo $row['ISBN'];?></td>
<td> <?php echo $row['Title'];?></td>
<td> <?php echo $row['Author'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
最佳答案
在您的 html 表单中有:
<input type="submit" name="Search" value="Filter">
Search
变量,但在 php 中你检查 search
。 搜索
!= 搜索
您没有检查变量 valueToSearch
。它可以是空的。
请求将失败。
关于 concat
说 chris85。为什么需要这个?
关于php 搜索不显示数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37216405/