Car*_* M. 3 php mysql sql select
首先,我想提一提的是,我一直在疯狂地尝试并寻找解决方案,但到目前为止还没有运气。我的问题如下:
我有一个包含数十行的MySQL数据库,并创建了一个jQuery网格来显示数据。此页面已经可以使用了。根据要求,我整理了一个页面,人们可以在其中从复选框中选择几个选项并过滤结果。它是这样工作的:
第1页有3组复选框(名称,位置,语言),每组都有不同的选项([名称:David,Nick等],[位置:纽约,华盛顿,孟买,伦敦,迈阿密],[语言: [英文,西班牙文]。一旦用户选择了自己的选项,它将提交一个包含以下内容的表格
grid.php?location = Washington&location =孟买&language = English&name = David
创建字符串很容易,但是接收器将使用此参数,然后将其放置在数组中然后创建SQL语句,这是我努力的地方。
我已经尝试过这样的事情:
$location= $_GET['location'];
$language= $_GET['language'];
$name= $_GET['name'];
if($location!=""){
$where[] = 'location=';
$where_args[] = $location;
}
//same code for the other 2 options
$where_clause = implode(' OR ', $where_args);
$sql_json = "SELECT * FROM myTable WHERE $where_clause ORDER BY id DESC";
Run Code Online (Sandbox Code Playgroud)
没有一个选项是强制性的,因此查询需要接受是否可以设置或不设置该值的可能性,因此最终可能是grid.php?language = English之类的东西。
我知道上面的代码还不能正常工作,但是如果有人可以向我指出正确的方向,我将不胜感激。
小智 5
尝试这个:
<?php
$location= $_GET['location'];
$language= $_GET['language'];
$name= $_GET['name'];
if($location!=""){
$where[] = " `location` = '".mysql_real_escape_string($location)."'";
}
if($language!=""){
$where[] = " `language` = '".mysql_real_escape_string($language)."'";
}
if($name!=""){
$where[] = " `name` = '".mysql_real_escape_string($name)."'";
}
$where_clause = implode(' OR ', $where);
//same code for the other 2 options
$sql_json = "SELECT * FROM myTable WHERE $where_clause ORDER BY id DESC";
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9029 次 |
| 最近记录: |