Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Mysqli高级搜索(多选项)_Php_Search_Mysqli - Fatal编程技术网

PHP Mysqli高级搜索(多选项)

PHP Mysqli高级搜索(多选项),php,search,mysqli,Php,Search,Mysqli,我正在尝试创建一个多选项搜索。我到处都找过,似乎找不到合适的解决方案,尽管很多网站都有。 因此,我有我的html表单(为了不让您感到厌烦,它被截断了一点): 提前非常感谢你们的帮助。任何建议都将不胜感激。我正在努力学习尽可能多的东西,但我真的被这件事困住了 更新:所以我做了一些创造性的实验,我觉得我很接近,只是无法让它工作: $query = array(); if (!empty($_GET['vType'])) { $query[] = "vType='$vType'"; } if (!em

我正在尝试创建一个多选项搜索。我到处都找过,似乎找不到合适的解决方案,尽管很多网站都有。 因此,我有我的html表单(为了不让您感到厌烦,它被截断了一点):

提前非常感谢你们的帮助。任何建议都将不胜感激。我正在努力学习尽可能多的东西,但我真的被这件事困住了

更新:所以我做了一些创造性的实验,我觉得我很接近,只是无法让它工作:

$query = array();
if (!empty($_GET['vType'])) {
$query[] = "vType='$vType'";
}
if (!empty($_GET['make'])) {
$query[] = "make='$make'";
}
if (!empty($model)) {
$query[] = "model='$model'";
}
if (!empty($yearfrom)) {
$query[] = "minyear >'$yearfrom'";
}
if (!empty($yearto)) {
$query[] = "maxyear>'$yearto'";
}
if (!empty($minprice)) {
$query[] = "minPrice >'$minprice'";
}
if (!empty($maxprice)) {
$query[] = "maxyear >'$maxprice'";
}
if (!empty($location)) {
$query[] = "location='$location'";
}
if (empty($query)){
$where = "WHERE";
}else{
$where = "";
}   
$query = implode(' AND ', $query);
$sql = "SELECT * FROM searchdata $where $query";

$result = mysqli_query($conDB, $sql);
if (mysqli_num_rows($result) === 0) {
echo 'Nothing Found';
}
while ($row = $result->fetch_assoc()) {
echo $row['vType'];

我肯定我在这里做错了什么。谢谢大家,我真的很感谢你们的帮助

你们可以使用isset和php脚本来构建你们的查询。大概是这样的:

$query = "select *  search_data where";

if(isset($make) && isset($model)){
   //do something
  $query .= "something you want to do ";
}

你也可以这样做

$sql = "select * from table_name  where column = $param1"

if(isset($param2))
{
 $sql=. "and column2 = $param2"
}`
if(isset($param3))
{
 $sql=. "and column3 = $param3"
}`
if(isset($param4))
{
 $sql=. "and column4 = $param4"
}`

几个小时后我才明白。我想我会为你发布代码,以防其他人面临同样的问题。再次感谢您的回复: $query=array()

if(!empty($\u GET['vType'])){
$vType=$\u获取['vType'];
$query[]=“vType=”$vType';
}
如果(!empty($_GET['make'])){
$make=$_GET['make'];
$query[]=“make=”$make';
}
如果(!empty($\u GET['model'])){
$model=$_GET['model'];
$query[]=“model=”$model';
}
如果(!empty($_GET['minyear'])){
$yearfrom=$_GET['minyear'];
$query[]=“年数>=”$yearfrom';
}
如果(!empty($\u GET['maxyear'])){
$yearto=$_GET['maxyear'];
$query[]=“年份=”$minprice';
}
如果(!empty($\u GET['maxPrice'])){
$maxprice=$_GET['maxprice'];

$query[]=“VPRICED您是否有提交按钮?请明确说明您希望如何构建您的查询!谢谢。我确实想过(并尝试过)类似的方法,但访问者可以通过多种方式进行搜索(仅使用make、make和location、location和maxprice等。也许有一种方法可以设置所有变量并从数组中创建字符串?@FelipeFernándezánchez访问者可以通过多种方法进行搜索-有些条件需要严格比较,有些子字符串搜索,有些则需要或多或少的比较和信息。)r有些你需要从集合中选择。你想用什么样的数组来保存所有这些信息?如果我首先使用If(isset($\u get['make'];$query=“make=$make”)获取所有变量,那该怎么办"然后不知何故用and内爆并爆炸?因此最终的查询将是SELECT*FROM searchdata WHERE$make?想想看,不需要爆炸。我能不能用and内爆?然后,如果没有设置变量,剩下的就是从查询中删除WHERE:SYes,就是这样!它与其他答案有什么不同?我的参数是差异
$query = "select *  search_data where";

if(isset($make) && isset($model)){
   //do something
  $query .= "something you want to do ";
}
$sql = "select * from table_name  where column = $param1"

if(isset($param2))
{
 $sql=. "and column2 = $param2"
}`
if(isset($param3))
{
 $sql=. "and column3 = $param3"
}`
if(isset($param4))
{
 $sql=. "and column4 = $param4"
}`
if (!empty($_GET['vType'])) {
$vType = $_GET['vType'];
$query[] = "vType='$vType'";
}
if (!empty($_GET['make'])) {
$make = $_GET['make'];
$query[] = "make='$make'";
}
if (!empty($_GET['model'])) {
$model = $_GET['model'];
$query[] = "model='$model'";
}
if (!empty($_GET['minyear'])) {
$yearfrom = $_GET['minyear'];
$query[] = "years>='$yearfrom'";
}
if (!empty($_GET['maxyear'])) {
$yearto = $_GET['maxyear'];
$query[] = "years<='$yearto'";
}
if (!empty($_GET['minPrice'])) {
$minprice = $_GET['minPrice'];
$query[] = "vPrice>='$minprice'";
}
if (!empty($_GET['maxPrice'])) {
$maxprice = $_GET['maxPrice'];
$query[] = "vPrice<='$maxprice'";
}
if (!empty($_GET['location'])) {
$location = $_GET['location'];
$query[] = "location='$location'";
}
if (!empty($query)){
$where = "WHERE";
}else{
$where = "";
}

$searchquery = implode(' AND ', $query);
$getsearch = "SELECT * FROM searchdata $where $searchquery";

$ressearch = mysqli_query($conDB, $getsearch);
if (mysqli_num_rows($ressearch) === 0) {
echo 'Nothing Found';
}else{
while($r = $ressearch->fetch_assoc()) {
//echo results here...
}