获取多个ID,同时将多个选择选项值POST到下一个表单

时间:2015-10-05 03:26:21

标签: php arrays implode multipleselection

如何在将多个选择选项值POST到下一个表单时获取多个ID?我只从数组中获取第一个选择ID。你们可以向我提出任何想法吗?

选择值时,这是我的代码。

 <tr>
                  <label>Auditor: </label>
                     <select class="form-control" name="auditor[]" multiple="multiple" >
                    <?php 
                    $result =  $db->query("SELECT * FROM auditor"); 
                    while($row = mysqli_fetch_array($result))
                    {
                        echo '<option value="'.$row["auditor_name"].'">'.$row["auditor_name"].'</option>';
                        }   
                    echo "</select>";
                    ?>
                  </tr>

这是POST到下一页时的另一个代码。

$myselected         =   $_POST["auditor"];

if(count($myselected)>1){
    $auditor = implode ("','",$myselected);
    }else{
    $auditor =$myselected;
    }

$query10 = "SELECT * FROM auditor WHERE auditor_name IN ('$auditor') ";
$result10 = $db->query($query10);
$row10 = $result10->fetch_array();

?>

<form action="audit_action/audit_action.php" role="form" method="post" name="auditformdetails" onsubmit="return(validate());">  
<table width='100%' border='0' class="table">
<tr>
    <td colspan=6>Audit details</td>
    <td colspan=6>Outlet details</td>
</tr>

<tr>
<td><b>Auditor:</b></td>

<td colspan='5'>
**<?php 
    echo'<input type="hidden" name="auditor_id" value="'.$row10["id"].'">';

    foreach ($myselected as $auditor){ 
    echo $auditor."<br>\n"; 
    }
?>**
</td>

1 个答案:

答案 0 :(得分:0)

您无法将字符串与mysql IN Clause进行比较。因此,您必须将您的每个值与查询中的条件或条件连接起来,如下所示。

$myselected  =   $_POST["auditor"];
$sql_cond = "";
if(count($myselected)>1){

    foreach($myselected  as $selected){
        if($sql_cond != "")
          $sql_cond.=" or auditor_name = ".$selected;
        else 
            $sql_cond.=" auditor_name = ".$selected;
    }

}else{
     $auditor =$myselected;
}

$query10 = "SELECT * FROM auditor WHERE ".$sql_cond;