这是我的 PHP 代码:
if (($_REQUEST['Search']) || ($_REQUEST['CSV'])){
$street = $_REQUEST['street'];
$zip = $_REQUEST['zip'];
$city = $_REQUEST['city'];
$type = $_REQUEST['type'];
if ($_REQUEST['street']){
$q = " SELECT * FROM $usertable WHERE address LIKE '%$street%' ORDER BY address ";
}
if ($_REQUEST['zip']){
$q = " SELECT * FROM $usertable WHERE zip LIKE '%$zip%' ORDER BY address ";
}
if ($_REQUEST['city']){
$q = " SELECT * FROM $usertable WHERE city LIKE '%$city%' ORDER BY address ";
}
$qu = mysql_query($q);
这是我的 html:
<form action="" method="post">
Zip:<input name="zip" type="text" />
Street:<input name="street" type="text" />
City:<input name ="city" type="text" />
Type:<select id="type" name="type">
<option value="Invasion">Invasion</option>
<option value="Burglary">Burglary</option>
<option value="Theft">Theft</option>
</select>
<input name="Search" type="submit" value="Search" />
<input name="CSV" type="submit" value="Get CSV" />
</form>
我想做的是让我的网站可以使用城市 zip 或街道的任意组合进行搜索。我确定我必须进行连接或其他操作,但作为一名后院程序员,我有点难过。谢谢!
最佳答案
在 1 个查询中获取所有内容(未测试);
<?php
$q = " SELECT * FROM ". $usertable ." WHERE 1 %s ORDER BY address ";
if ($_REQUEST['street']){
$where[] = " address LIKE '%".$street."%' ";
}
if ($_REQUEST['zip']){
$where[] = " zip LIKE '%".$zip."%' ";
}
if ($_REQUEST['city']){
$where[] = " city LIKE '%".$city."%' ";
}
$q = sprintf($q, implode(" AND ", $where));
echo $q;
关于php - 如何使用多个输入通过一次搜索来搜索多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9216476/