gpt4 book ai didi

php - 带有下拉类别的搜索栏。搜索结果未从数据库中提取并显示

转载 作者:行者123 更新时间:2023-11-29 17:32:07 25 4
gpt4 key购买 nike

所以我有搜索栏,我希望搜索 mysql 数据库中的记录并将其显示在网页上。它应该允许用户选择他们正在搜索的字段,但它不会显示另一端的记录。有什么想法吗?

html:

	<form action='recordresult.php' method='POST' name='form_filter' class="form-style-1" >    
<b>Search</b><br>
<select name="selectVal">
<option value="category" >Select a category</option>
<option value="first_name">First Name</option>
<option value="surname">Surname</option>
<option value="address">Address</option>
<option value="phonenumber">Telephone</option>
</select>
<input type='text' name='search' placeholder='Enter text here...'><br>
<input type='submit' value='Send'>
</form>

PHP

<?php
include("config.php");
$link = mysqli_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysqli_error($link));

// select the database
mysqli_select_db($link, $database)
or die ("Could not select database because ".mysqli_error($link));

$search = isset($_POST['search']) ? htmlspecialchars(trim($_POST['search'])) : null;
$catLocation = isset($_POST['selectVal']) ? htmlspecialchars(trim($_POST['selectVal'])) : null;
$query = "SELECT * FROM $table WHERE ";

//YOU INDICATED YOU'D NEED TO RUN THE SEARCH-QUERY IF THE SEARCH-TERM AND SEARCH-SCOPE ARE DEFINED IE: NOT NULL; HOWEVER IF THE SEARCH TERM IS NOT GIVEN, YOU SELECT EVERYTHING IN THAT TABLE... (BAD PRACTICE, THOUGH)
if($catLocation){
if($search){
if($catLocation == "category"){
$query .= " category LIKE '%" . $search . "%'";
}
else if($catLocation == "first_name"){
$query .= "first_name LIKE '%" . $search . "%'";
}
else if($catLocation == "surname"){
$query .= "surname LIKE '%" . $search . "%'";
}
else if($catLocation == "address"){
$query .= "address LIKE '%" . $search . "%'";
}
else if($catLocation == "phonenumber"){
$query .= "phonenumber LIKE '%" . $search . "%'";
}
}

else{
$query .= "1";
}

$sql = mysqli_query($query);
//HERE AGAIN WAS AN ERROR... YOU PASSED mysql_fetch_array A STRING $query INSTEAD OF A RESOURCE: $sql
while ($row = mysqli_fetch_array($sql)){
$firstname = $row["first_name"];
$surname = $row["surname"];
$address = $row["address"];
$phonenumber = $row['phonenumber'];


echo "First Name : $firstname<br>";
echo "Surname : $surname<br>";
echo "Address : $address<br>";
echo "Phone Number: $phonenumber<br>";

}

}

?>

代码没有提供任何错误,只是在应该出现的地方出现了空白区域。还想知道是否有人知道是否可以将名字和姓氏作为字段并搜索“Emma Watson”,并且如果其中一个单词在其中,则能够从这两个字段返回结果?

感谢您的帮助!

最佳答案

请检查下面更新的代码

include("config.php");
$link = mysqli_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysqli_error($link));

// select the database
mysqli_select_db($link, $database)
or die ("Could not select database because ".mysqli_error($link));

$search = isset($_POST['search']) ? htmlspecialchars(trim($_POST['search'])) : null;
$catLocation = isset($_POST['selectVal']) ? htmlspecialchars(trim($_POST['selectVal'])) : null;
$query = "SELECT * FROM $table WHERE ";

//**If you want to merge for first name and surname then you need to merge both query with OR condition as below**
if($catLocation){
if($search){
if($catLocation == "category"){
$query .= " category LIKE '%" . $search . "%'";
}
else if($catLocation == "name"){
$query .= " ( first_name LIKE '%" . $search . "%' OR surname LIKE '%" . $search . "%' ) ";
}
else if($catLocation == "address"){
$query .= "address LIKE '%" . $search . "%'";
}
else if($catLocation == "phonenumber"){
$query .= "phonenumber LIKE '%" . $search . "%'";
}
}
else{
$query .= "1";
}

$sql = mysqli_query($link, $query); // **Adding reference connection variable**

while ($row = mysqli_fetch_array($sql)){
$firstname = $row["first_name"];
$surname = $row["surname"];
$address = $row["address"];
$phonenumber = $row['phonenumber'];


echo "First Name : $firstname<br>";
echo "Surname : $surname<br>";
echo "Address : $address<br>";
echo "Phone Number: $phonenumber<br>";

}

}

将 2 个字段(名字和姓氏)合并为一个(名称),以便在两个字段中进行搜索

<form action='recordresult.php' method='POST' name='form_filter' class="form-style-1" >    
<b>Search</b><br>
<select name="selectVal">
<option value="category" >Select a category</option>
<option value="name">name</option>
<option value="address">Address</option>
<option value="phonenumber">Telephone</option>
</select>
<input type='text' name='search' placeholder='Enter text here...'><br>
<input type='submit' value='Send'>
</form>

关于php - 带有下拉类别的搜索栏。搜索结果未从数据库中提取并显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50557296/

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