我正在尝试使用HTML表单的搜索栏和PHP从数据库中搜索特定记录,以获取后端查询。这是我的HTML表单代码:
<form style="float: right;margin-right: 80px;margin-top: 30px;" action="search_file.php" method="get">
<input type="search" name="search">
</form>
这是我用于搜索机制或过程的PHP代码:
<?php
$search=$_GET['search'];
$stores = "select * From uploadfile";
$stores_sql = mysqli_query($conn, $stores);
while($row1 = mysqli_fetch_array($stores_sql)){
$name = $row1['name'];
$city = $row1['city'];
$email = $row1['email'];
$phone = $row1['phone'];
$file = $row1['uploaded_file'];
$address=$row1['address'];
if($search==$name){
echo"<tr><td>$name</td><td>$city</td><td>$phone</td><td>$email</td><td><a href='uploads/$file' download>Download the file</a></td><td>$address</td></tr>";
}
elseif($search==$city){
echo"<tr><td>$name</td><td>$city</td><td>$phone</td><td>$email</td><td><a href='uploads/$file' download>Download the file</a></td><td>$address</td></tr>";
}
elseif($search==$email){
echo"<tr><td>$name</td><td>$city</td><td>$phone</td><td>$email</td><td><a href='uploads/$file' download>Download the file</a></td><td>$address</td></tr>";
}
else{
echo "keep trying!!!";
}
}
?>
将查询更改为
"select * From uploadfile where name = '".$search."' or city = '".$search."' or email = '".$search."'";
,或者您可以使用像查询一样获取类似于搜索术语的所有记录
"select * From uploadfile where name like('%".$search."%)' or city like('%".$search."%)' or email like('%".$search."%)'";
使用此代码进行搜索:
$search=$_GET['search'];
$stores = "select * From uploadfile WHERE name = '".$search."' OR city = '".$serach."' OR email ='".$serach."'";
$stores_sql = mysqli_query($conn, $stores);