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

时间:2017-05-13 06:26:26

标签: php mysql

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

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

目标是允许用户搜索多个字段以返回匹配项。所以Colour1和Postcode



<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>
&#13;
&#13;
&#13;

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

&#13;
&#13;
<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');
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

玩了一会儿,找到了答案。我需要定义标准。见下面的代码

&#13;
&#13;
<?php 

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

// connect to the database

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

$query = "SELECT * FROM `Mainlist` WHERE (`Colour1`like '%$criteria%') and (`Colour2`like '%$criteria2%') 
and (`PostCode`like '%$criteria3%') and (`Suburb`like '%$criteria4%') and (`State`like '%$criteria5%')
&#13;
&#13;
&#13;