gpt4 book ai didi

php 搜索不显示数据库记录

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:11 24 4
gpt4 key购买 nike

我在 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>&nbsp;<?php echo $row['ISBN'];?></td>
<td>&nbsp;<?php echo $row['Title'];?></td>
<td>&nbsp;<?php echo $row['Author'];?></td>
</tr>
<?php endwhile;?>

</table>
</form>
</body>
</html>

最佳答案

在您的 html 表单中有:

<input type="submit" name="Search" value="Filter">

Search 变量,但在 php 中你检查 search搜索 != 搜索


您没有检查变量 valueToSearch。它可以是空的。请求将失败。


关于 concatchris85。为什么需要这个?

关于php 搜索不显示数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37216405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com