PHP 中的多个搜索参数
我有一个表单,允许输入几种不同的搜索选项。例如文本框和下拉菜单。截至目前,我通过等待搜索请求然后查看各个文本框中是否有任何字符输入或选择了哪些下拉选项来进行搜索。
基本上它是使用 if 语句的条件逻辑分支,然后根据选择的内容修改 sql 语句。我怎样才能让它支持单个选项或多个链接在一起?
当前条件逻辑的示例
$sql = "SELECT ";
if(strlen($_REQUEST['Option1']) > 0) {
$sql .= "* FROM Table Where Option1 = {$_REQUEST['Option1']} ";
}
if(strlen($_REQUEST['Option 2']) > 0) {
$sql .= "* From Table Where Option2 >= {$_REQUEST['Option2']}";
我知道我需要在 sql 查询中使用 AND。我尝试使用选项数组来完成此操作,然后根据布尔状态进行循环,但它不起作用
I have a form that allows for several different search option inputs. Such as text boxs and drop downs. As of now I search by waiting for a search REQUEST and then seeing if there is any character input in the various text boxes or what drop down option is selected.
Basically it is branches of conditional logic using if statements then modifying the sql statement depending what is chosen. How can I make it to where it supports single options or several linked together?
Example of current condititional logic
$sql = "SELECT ";
if(strlen($_REQUEST['Option1']) > 0) {
$sql .= "* FROM Table Where Option1 = {$_REQUEST['Option1']} ";
}
if(strlen($_REQUEST['Option 2']) > 0) {
$sql .= "* From Table Where Option2 >= {$_REQUEST['Option2']}";
I understand I need to use AND in the sql query. I tried to do it with arrays of options then looping through depending on the boolean state but it didn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 where 子句放入数组中,然后使用 OR 或 AND 进行内爆:
$sql .= implode(" AND ", $whereArray);
Put the where clauses in an array then do an implode with OR or AND:
$sql .= implode(" AND ", $whereArray);