简体   繁体   English

如何选择多个下拉菜单的所有选项并在php和mysql中执行查询? 更简单的方法?

[英]How to select all option for multiple dropdowns and perform query in php and mysql? easier method?

I am new to php and sql so please bear with me. 我是php和sql的新手,所以请多多包涵。 I did try and search but did not find quite wat i am looking for. 我确实尝试搜索,但没有找到我想要的水。 Hope u can help me out. 希望你能帮助我。 so here goes! 所以这里去!

I have a certain number of drop down menus with values. 我有一定数量的带有值的下拉菜单。 I like to include a select all option which will select all the options and does an sql query for all values. 我喜欢包括一个全选选项,它将选择所有选项并对所有值进行sql查询。 I have wriiten a sample code using if conditions to perform the query but the number of if cond increase exponentially based on the number of drop down menus. 我写了一个示例代码,使用if条件执行查询,但是if cond的数量根据下拉菜单的数量呈指数增长。 2^n im guessing. 我猜是2 ^ n。 I wanted to know if there is a better way of doing this. 我想知道是否有更好的方法可以做到这一点。 please find the code below. 请在下面找到代码。

$sql = mysql_query("SELECT distinct `Num. of Parts` from `$verb`");
echo "<select name='filter1'>";
echo "<option value='1'> Select all </option>";
while($row = mysql_fetch_assoc($sql))
{
$filter1 = $row['Num. of Parts'];
echo "<option value='$filter1'> $filter1 </option>";
}
echo "</select>";
$sql = mysql_query("SELECT distinct `Type of positioning` from `$verb`");
echo "<select name='filter2'>";
echo "<option value='1'> Select all </option>";
while($row = mysql_fetch_assoc($sql))
{
$filter2 = $row['Type of positioning'];
echo "<option value='$filter2'> $filter2 </option>";
}
echo "</select>";
echo "<input type=\"submit\" name=\"submitC\" value=\"SUBMIT\">";

the submitC value is passed here: 在这里传递submitC值:

if($filter1==1 && $filter2!=1)
{$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning`='$filter2' and `Distance range`='$dist'");
}
elseif($filter1!=1 && $filter2==1)
{$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning` like '%' and `Distance range`='$dist'");
}
elseif($filter1==1 && $filter2==1)
{$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning` like '%' and `Distance range`='$dist'");
}
else
{
$sql = mysql_query("SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning`='$filter2' and `Distance range`='$dist'");
}

Thanks in advance guys! 在此先感谢大家! ps EXCLUDE $dist ps排除$ dist

you can select multiple option by name='filter1[]' 您可以通过name='filter1[]'选择多个选项

and you can store these by $filter1= serialize($_POST['filter1']) ; 您可以通过$filter1= serialize($_POST['filter1']) ;来存储它们$filter1= serialize($_POST['filter1']) ;

try like this 这样尝试

if($filter1==1) {  if(&& $filter2!=1){
$sql = "SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning`='$filter2' and `Distance range`='$dist'"; }
elseif($filter2==1){$sql = "SELECT * from `$verb` where `Num. of Parts` like '%' and `Type of positioning` like '%' and `Distance range`='$dist'";  }  }

elseif($filter1!=1 && $filter2==1) { 
$sql = "SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning` like '%' and `Distance range`='$dist'";  }

else{
$sql = "SELECT * from `$verb` where `Num. of Parts`='$filter1' and `Type of positioning`='$filter2' and `Distance range`='$dist'";  }

$result = mysql_query($sql);

If i got you right you are searching something like this : 如果我说对了,您正在搜索如下内容:

<?php

$mapping = array ( 'column1' => 'Num_of_Parts' , 
      'column2' => 'Type_of_positioning'
);  
    $sql = "select * from table ";
    $connector = " AND ";
    if ( isset($_POST['filters']) ) {
        $sql = $sql . "where ";
        foreach ($_POST['filters'] as $key=>$val) {
            $sql .= $mapping[$key] ." like '%"  . $val. "%'" . $connector ; 
        }

    }//end of if
    $sql.= " Distance range = 'value'";
    var_dump($sql);


?>


<html>
<body>

<form action="" method="post">
<select name = "filters[column1]">
  <option value="">Select All</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select name = "filters[column2]">
  <option value="">Select All</option>
  <option value="saab">Car</option>
  <option value="mercedes">Bus</option>
  <option value="audi">Cycle</option>
</select>

<input type="submit" value="submit">
</form>

</body>
</html>

Note 注意

  1. Always sanitize user inputs by using mysqli_real_escape_string or something similar like this but do not use mysql family functions as they are encouraged not to use in php doc . 始终使用mysqli_real_escape_string或类似的方法来清理用户输入,但不要使用mysql系列函数,因为建议不要在php doc中使用它们。
  2. Do not use column names as the name of the elements for security purpose. 为了安全起见, 请勿将列名用作元素的名称。 You can see in the code how to remap column names by using just a simple trick. 您可以在代码中看到如何通过简单的技巧重新映射列名。

Happy coding :) 快乐的编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM