允许未选择的选项的{s}搜索

时间:2016-11-07 23:24:46

标签: php mysql

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

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 } ?>

但它非常详细,并未涵盖所有搜索选项,例如如果用户选择国家/地区和类型,除非我为每个特定搜索编写代码,否则不会覆盖上述内容。

1 个答案:

答案 0 :(得分:0)

考虑动态构建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

旁注,我不确定为什么你的代码中散布了所有?> </php对。他们只能伤害他们。