gpt4 book ai didi

php - 搜索多个条件 - 和/或在 PHP 中搜索

转载 作者:行者123 更新时间:2023-11-29 18:59:50 26 4
gpt4 key购买 nike

不确定我在这里做错了什么。我想创建一个允许用户进行“和”或“搜索”的搜索。

但是,当我使用下面的代码时,如果我输入 Brown 作为 Colour1,它将返回所有结果,与邮政编码相同。

目标是允许用户搜索多个字段以返回匹配项。所以 Color1 和邮政编码

<html>
<head>
<title> Logo Search</title>
<style type="text/css">

table {
background-color: #FCF;
}

th {
width: 250px;
text-align: left;
}

</style>
</head>
<body>
<h1> National Logo Search</h1>


<form method="post" action="singlesearch2.php">
<input type="hidden" name="submitted" value="true"/>


<label>Colour 1: <input type="text" name="criteria" /></label>

<label>Colour 2: <input type="text" name="criteria2" /></label>

<label>PostCode: <input type="text" name="criteria3" /></label>

<label>Suburb: <input type="text" name="criteria4" /></label>
<input type="submit" />


</form>

<?php

if (isset($_POST['submitted'])) {

// connect to the database

include('connect.php');
//echo "connected " ;
$criteria = $_POST['criteria'];

$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%')

or
('Colour2' like '%$criteria2%')
or
('PostCode' = '%$criteria3%')
or
('Suburb' like '%$criteria4%')


LIMIT 0,5";
$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');

echo "<table>";

echo "<tr> <th>School</th> <th>State</th> <th>Suburb</th> <th>PostCode</th> <th>Logo</th> <th>Uniform</th></tr>";

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

echo "<tr><td>";
echo $row['School'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Suburb'];
echo "</td><td>";
echo $row['PostCode'];
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Logo']);
echo "\" /></td></td>";
echo "</td><td><img src=\"data:image/jpeg;base64,";
echo base64_encode($row['Uniform']);
echo "\" /></td></td>";

}

echo "</table>";



}// end of main if statment

?>


</body>
</html>

当我使用下拉列表选择条件时,我可以让它正常工作,但我希望它们有多个选项来过滤结果。

<form method="post" action="multisearch.php">
<input type="hidden" name="submitted" value="true"/>

<label>Search Category:

<select name="category">
<option value="Colour1">Main Colour</option>
<option value="Colour2">Secondary Colour</option>
<option value="PostCode">Post Code</option>
</select>

<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />


</form>

<?php

if (isset($_POST['submitted'])) {

// connect to the database

include('connect.php');
echo "connected " ;
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM `Mainlist` WHERE $category LIKE '%$criteria%'";

$result = mysqli_query($dbcon, $query) or die(' but there was an error getting data');

最佳答案

通常,您使用 AND 条件运行查询,因为它有条件,并且您拥有它意味着您必须通过将每个条件与接受列匹配来显示值。但这也取决于用户或客户想要如何展示自己的领域。

简而言之,它没有任何规则如何显示。

关于php - 搜索多个条件 - 和/或在 PHP 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43949958/

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