【问题标题】:get multiple ID while POST multiple selection option value to next form在将多项选择选项值发布到下一个表单时获取多个 ID
【发布时间】:2016-01-01 15:38:27
【问题描述】:

如何在将多项选择选项值发布到下一个表单时获取多个 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>

【问题讨论】:

  • 你能解释一下你想要什么输出吗?
  • 嗨@sandeepsure,输出例如:ROBERT ALAN ALVIN 但是,当发布数据时,它只显示第一个 ROBERT“id”;其他两个 ALAN 和 ALVIN id 未显示,当我传递表单时,它只将第一个 ROBERT id 存储到数据库中。
  • print_r($myselected ) 告诉我结果。
  • @sandeepsure Array ([0] => Michiyo (WY) [1] => Shiow Yong (BUN) [2] => Seok Hoon (SH))

标签: php arrays implode multipleselection


【解决方案1】:

你不能用 mysql IN 子句比较字符串。因此,您必须将每个值与查询中的或条件连接起来,如下所示。

$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;

【讨论】:

  • 查询后的$result语句呢?刚刚跟随 $result10 = $db->query($query10); $row10 = $result10->fetch_array();对吗?
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 2012-01-11
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
相关资源
最近更新 更多