多选下拉列表

时间:2017-01-28 09:12:02

标签: php mysql

我在其中一个输入中有一个多输入搜索字段的搜索页面我在我的状态中使用多选下拉列表显示在下面我可以选择多个字来对其进行搜索 此多选下拉列表来自Here

表单如下:

    <form class="form form-inline form-multiline" role="form" name="form" method="post" action="<?php $_PHP_SELF ?>">
      <div class="form-group">
        <label for="pwd">Type</label>
        <select id="selectbasic" name="txt_type" class="form-control">
      <option value="">--SELECT--</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Inspection') { ?>selected="true" <?php }; ?>value="Inspection">Inspection</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Service') { ?>selected="true" <?php }; ?>value="Service">Service</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Project') { ?>selected="true" <?php }; ?>value="Project">Project</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Appointment') { ?>selected="true" <?php }; ?> value="Appointment">Appointment</option>
          <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Email') { ?>selected="true" <?php }; ?> value="Email">Email</option>
           <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Phone Call') { ?>selected="true" <?php }; ?> value="Phone Call">Phone Call</option>
           <option<?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Service Activity') { ?>selected="true" <?php }; ?> value="Service Activity">Service Activity</option>
        <option <?php if (isset($_POST['txt_type']) && $_POST['txt_type'] == 'Task') { ?>selected="true" <?php }; ?> value="Task">Task</option> 
        </select>
      </div>
     <div class="form-group">
        <label for="pwd">Status</label>
        <select  name="txt_status[]"  multiple id="langOpt">
          <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Scheduled') { ?>selected="true" <?php }; ?>value="Scheduled">Scheduled</option>
          <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Cancelled') { ?>selected="true" <?php }; ?>value="Cancelled">Cancelled</option>
          <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Pending') { ?>selected="true" <?php }; ?>value="Pending">Pending</option>
           <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'Done') { ?>selected="true" <?php }; ?>value="Done">Done</option>
            <option <?php if (isset($_POST['txt_status']) && $_POST['txt_status'] == 'In Progress') { ?>selected="true" <?php }; ?>value="In Progress">In Progress</option>
        </select>
      </div>
</form>

多重选择位于状态字段

了解PHP搜索代码

if(isset($_POST['submit'])) {
$q = array(); 
    $sql = "";
      if(isset($_POST['txt_type']) && !empty($_POST['txt_type'])){  
      $type =  mysqli_real_escape_string($conn,$_POST['txt_type']); 
    $q[] = " db_type='".$type."' ";      
    } 
    if(isset($_POST['txt_status']) && !empty($_POST['txt_status'])){ 
    $status = $_POST['txt_status'];
    $a=implode(",",$status);       
    $q[] = " db_status='".$a."' ";      
    } 
    $first = true;     
    foreach($q as $qu){  
        if($first){  
        $sql .= " where ".$qu;      
        $first = false;  
        }else{  
        $sql .= " and ".$qu;          
        } 
    } 
    $result=mysqli_query($conn,"SELECT * FROM tbl_staff {$sql}  ")or die(mysqli_error($conn));
}

知道我是否打印查询,查询将是这样的

SELECT * FROM tbl_staff where db_status='Scheduled,Cancelled'

问题是","

中有db_status='Scheduled,Cancelled'

但它应该是这样的

SELECT* FROM TABLE WHERE db_status='Scheduled' and db_status='Cancelled'

在这种情况下,我选择的状态可能是我选择一个或三个或更多

如何更正查询以获得我想要的确切结果?

2 个答案:

答案 0 :(得分:1)

将您的内爆转换为此并使用in并尝试:

$a="'". implode("', '", $status) ."'";    // gives like '1', '2'

喜欢:

$q[] = " db_status in($a) ";      

<强> BUT

如果你想使用,你可以这样做:

foreach($status as $k=>$v){   
     $q[] = " db_status='".$v."' ";      //use this in your other loop.
    } 

答案 1 :(得分:0)

试试这个解决方案:

if(isset($_POST['txt_status']) && !empty($_POST['txt_status'])){ 
    $statuses = $_POST['txt_status'];
    foreach ($statuses as $status) {
        $q[] = " db_status='".$status."' ";      
    }

}