gpt4 book ai didi

php - 带下拉菜单的搜索表单

转载 作者:行者123 更新时间:2023-11-29 17:51:33 26 4
gpt4 key购买 nike

我正在尝试使用输入创建一个基本的高级搜索,然后该输入将搜索具有在下拉列表中选择的匹配类别字段的任何结果,然后还有“advancedSearch”中company_name的匹配关键字字段。我已经到了可以使用下拉菜单显示匹配数据的阶段,但我在使用搜索输入查询该数据时遇到了困难。

这是我来自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);
}

这段代码非常适合从下拉列表中回显匹配的类别,但我无法弄清楚如何从“高级搜索”输入中进一步查询搜索。

任何帮助将不胜感激。

最佳答案

我发现在这样的查询中使用 AND 并使用提交时的请求我能够得到我想要的东西......感谢@ADyson 的插入。我已在下面添加了更新的 advance-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);
}

关于php - 带下拉菜单的搜索表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49283462/

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