使用PHP和SQL进行多项多选(HTML)搜索

时间:2015-02-23 04:35:09

标签: php html sql

我陷入了一段我无法使其发挥作用的代码。我需要帮助。在这里:

我想用几个多选,PHP和SQL构建一个搜索。在例子中,我将使用02(两个)多选:



<form action="" method="post">
<!-- first select with its options-->
<select name="search[field1]" multiple="multiple">
  <option value="All">-- All --</option>
  <option value="value11">Value 11</option>
  <option value="value12">Value 12</option>
  <option value="value13">Value 13</option>
  <option value="value14">Value 14</option>
</select>
  
<!-- second select with its options -->
<select name="search[field2]" multiple="multiple">
  <option value="All">-- All --</option>
  <option value="value21">Value 21</option>
  <option value="value22">Value 22</option>
  <option value="value23">Value 23</option>
  <option value="value24">Value 24</option>
</select>
  
<!-- input submit -->
<input type="submit" value="Search" name="submit"/>
  
</form><!-- \. end form -->
&#13;
&#13;
&#13;

现在,我将表单提交给PHP,它与表单在同一页面中:

&#13;
&#13;
<?php

#first I check if the form was submitted
if(isset($_POST['submit'])){

  #catch the values from the multiple select forms
  $mapping = array (
      'field1' => 'Name_of_the_table_field_1' , 
      'field2' => 'Name_of_the_table_field_2'
  );

  $stmt = "select * from [DATA_BASE].[dbo].[TABLE] ";
  $operator = " AND ";

  if (isset($_POST['search'])){
    $stmt = $stmt . "where ";
    foreach ($_POST['search'] as $key=>$val) {
      $stmt .= $mapping[$key] ." =(case when ".$val." ='All' then ".$mapping[$key]."    else  '".$val."' end)" . $operator; 
    }
}

var_dump($stmt);


?>
&#13;
&#13;
&#13;

在这里,我卡住了!我不知道如何同时从两个多选中捕获多个值并将它们解析为SQL搜索。

&#13;
&#13;
E.g.: the search should return sth like "select * from [DATA_BASE].[dbo].[TABLE] where Name_of_the_table_field_1 = (case when Name_of_the_table_field_1 = 'All' then Name_of_the_table_field_1 else 'value11' AND "value12" end) AND Name_of_the_table_field_2 = (case when Name_of_the_table_field_2 = 'All' then Name_of_the_table_field_2 else 'value21' AND "value23" end)
&#13;
&#13;
&#13;

我可以通过逐个处理多选来处理这个问题,但最终会成为很大一部分代码,也很难理解。

我希望你们了解我,我真的需要帮助!

提前致谢!!!

PS:代码正在捕获每个多选中的第一个选项,但它没有保留其他选项。

1 个答案:

答案 0 :(得分:0)

我刚刚做了以下事情来解决我的问题。我认为这是用多种选择做事的虚拟方式,但这是我能做的。

我的表格:

&#13;
&#13;
<form action="" method="post">
  <select name="select1[]" multiple>
    <option value="value1">Value1</option>
    <option value="value2">Value2</option>    
    <option value="value3">Value3</option>
  </select>
  
  <select name="select2[]" multiple>
    <option value="value1">Value1</option>
    <option value="value2">Value2</option>    
    <option value="value3">Value3</option>
  </select>
  
  <select name="select2[]" multiple>
    <option value="value1">Value1</option>
    <option value="value2">Value2</option>    
    <option value="value3">Value3</option>
  </select>
  
  <input type="submit" name="search" value="Search!"/>
  
</form>
&#13;
&#13;
&#13;

&#13;
&#13;
if(isset($_POST['search'])){
    $select1 = implode(", ",$_POST['select1']);
    $select2 = implode(", ",$_POST['select2']);
    $select3 = implode(", ",$_POST['select3']);
    $select4 = "'" .implode("', '",$_POST['select4']). "'";
    $select5 = "'" .implode("', '",$_POST['select5']). "'";


    $query = "select * from [DATABASE].[dbo].[TABLE] where Column1 in ($select1) and Column2 in ($select2) and Column3 in ($select3) and Column4 in ($select4) and Column5 in ($select5)";

$exec = sqlsrv_query($conn, $query);

[...] so on
&#13;
&#13;
&#13;

这只是一个通用示例,但如果您指定表单和数据库的参数,它将起作用。

非常感谢你!