我正在尝试使用输入创建基本高级搜索,然后搜索在下拉列表中选择了匹配类别字段的任何结果,然后搜索" advancedSearch&#中company_name的匹配关键字字段34 ;.我已经到了可以使用下拉菜单然后显示匹配数据的阶段,但是我在查询输入时遇到了麻烦。
这是我在index.php
中的表单代码<form action="advanced-search.php" method="POST">
<input id="advancedInput" placeholder="Advanced Search" type="search" name="advancedSearch">
<?php
$sqlSelect="SELECT category FROM categories";
$result = $db -> query ($sqlSelect);
echo "<select id=\"selectAdvanced\" name=\"value\">";
echo "<option></option>";
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
foreach ($rows as $row) {
print "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
}
echo "</select>";
?>
<input type="submit" value="search"/>
</form>
以下是我的advanced-search.php
中的代码<?php
if(isset($_POST['value']) && !empty($_POST['value'])) {
$username = trim(strip_tags($_POST['value']));
include('dbConfig.php');
if (mysqli_connect_errno()) {
printf("Can't connect: %s\n", mysqli_connect_error());
exit();
}
$where = ($username == "category")? "" : " WHERE category = '$username'";
$sql = "SELECT * FROM company_listings" . $where; // Create Query
$result = mysqli_query($db, $sql); // Run Query
echo "<table border=1><tr><th>id</th><th>name</th><th>created</th></tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
此代码非常适合从下拉列表中回显匹配的类别,但我无法确定如何从&#34;高级搜索&#34;中进一步查询搜索。输入
非常感谢任何帮助。
答案 0 :(得分:0)
我在我的查询中使用AND并且使用提交时的请求我能够得到我之后的内容...感谢@ADyson的轻推。我在下面添加了更新的advanced-search.php文件;
if(isset($_REQUEST['submit'])){
$username = (($_POST['value']));
$advanced = (($_POST['advancedSearch']));
include('dbConfig.php');
if (mysqli_connect_errno()) {
printf("Can't connect: %s\n", mysqli_connect_error());
exit();
}
$sql=" SELECT * FROM company_listings WHERE company_name like '%".$advanced."%' AND category LIKE '%".$username."%'";
$result = mysqli_query($db, $sql); // Run Query
echo "<table border=1><tr><th>id</th><th>name</th><th>created</th></tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}