【问题标题】:Sql search that allows for unselected options允许未选择选项的 Sql 搜索
【发布时间】:2016-11-07 23:24:45
【问题描述】:

我有一个包含三个搜索选项的表单。

SELECT users.* FROM users WHERE users.genre = ? AND users.country = ? AND users.city = ?

此查询仅在满足所有三个条件时才返回结果

SELECT users.* FROM users WHERE users.genre = ? OR users.country = ? OR users.city = ?

仅当正确满足一个条件时,此查询才会返回结果

我需要做的是:

如果有人为国家/地区提交“澳大利亚”并将流派和城市留空,他们将获得澳大利亚的所有结果。

(city=Australia&genre=&country=)

如果有人为国家/地区提交“澳大利亚”,为流派提交“摇滚”并且留空,他们将获得澳大利亚和“摇滚”的所有结果。

(city=Australia&genre=&rockcountry=)

目前,如果我在表单上留空值,它不会给我任何结果,因为我认为它正在搜索 MySQL 中的空单元格?

感谢

 <php
$Recordset1 = new WA_MySQLi_RS("Recordset1",$alpha,1);
$Recordset1->setQuery("SELECT users.* FROM users WHERE users.genre = ? AND users.country = ? AND users.city = ?");
$Recordset1->bindParam("s", "".((isset($_POST["genre"]))?$_POST["genre"]:"")  ."", "-1"); //WAQB_Param1
$Recordset1->bindParam("s", "".((isset($_POST["country"]))?$_POST["country"]:"")  ."", "-1"); //WAQB_Param2
$Recordset1->bindParam("s", "".((isset($_POST["city"]))?$_POST["city"]:"")  ."", "-1"); //WAQB_Param3
$Recordset1->execute();
?>

认为这可能有效

<?php
if ((isset($_POST["country"]) && $_POST["country"] != '')) 
{?>
<?php
$Recordset1 = new WA_MySQLi_RS("Recordset1",$alpha,1);
$Recordset1->setQuery("SELECT users.* FROM users WHERE users.country = ?");
$Recordset1->bindParam("s", "".((isset($_POST["country"]))?$_POST["country"]:"")  ."", "-1"); //WAQB_Param2
$Recordset1->execute();
?>
<?php } ?>

<?php
if ((isset($_POST["country"]) && $_POST["country"] != '') && (isset($_POST["city"]) && $_POST["city"] != '')) 
{?>
<?php
$Recordset1 = new WA_MySQLi_RS("Recordset1",$alpha,1);
$Recordset1->setQuery("SELECT users.* FROM users WHERE users.genre = ? AND users.country = ? AND users.city = ?");
$Recordset1->bindParam("s", "".((isset($_POST["country"]))?$_POST["country"]:"")  ."", "-1"); //WAQB_Param2
$Recordset1->bindParam("s", "".((isset($_POST["city"]))?$_POST["city"]:"")  ."", "-1"); //WAQB_Param3
$Recordset1->execute();
?>
<?php } ?>


<?php
if ((isset($_POST["country"]) && $_POST["country"] != '') && (isset($_POST["city"]) && $_POST["city"] != '') && (isset($_POST["genre"]) && $_POST["genre"] != '')) 
{?>
<?php
$Recordset1 = new WA_MySQLi_RS("Recordset1",$alpha,1);
$Recordset1->setQuery("SELECT users.* FROM users WHERE users.genre = ? AND users.country = ? AND users.city = ?");
$Recordset1->bindParam("s", "".((isset($_POST["genre"]))?$_POST["genre"]:"")  ."", "-1"); //WAQB_Param1
$Recordset1->bindParam("s", "".((isset($_POST["country"]))?$_POST["country"]:"")  ."", "-1"); //WAQB_Param2
$Recordset1->bindParam("s", "".((isset($_POST["city"]))?$_POST["city"]:"")  ."", "-1"); //WAQB_Param3
$Recordset1->execute();
?>
<?php } ?>

但它非常冗长,并没有涵盖所有搜索选项,例如如果用户选择国家和类型,除非我为每个特定的搜索编写代码,否则上面不会涵盖。

【问题讨论】:

  • 如果在 queryString 中找不到,您能否尝试设置 NULL 值。并且还使用TRIM函数来寻找更好的结果

标签: php mysql


【解决方案1】:

考虑动态构建 WHERE 子句:

$sql = "SELECT * WHERE ";
if (isset($_POST["genre"]) {
    $sql .= 'users.genre = :genre OR ';
}
... same for other criteria

// chop off the extra ' OR ' on the end (simpler than tracking when to add one above)
$sql = substr($sql, -4);

$Recordset1->setQuery($sql);
if (isset($_POST["genre"])) {
    $Recordset1->bindParam(':genre', $_POST["genre"], PDO::PARAM_STR);
}
... same for other criteria

旁注,我不确定为什么您的代码中散布着所有这些 ?&gt; &lt;/php 对。他们除了伤害什么都做不了。

【讨论】:

    猜你喜欢
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2019-11-08
    • 2021-06-12
    • 2013-12-21
    • 2011-07-26
    相关资源
    最近更新 更多