处理多个选择选项中的数据(使用$ _POST)

时间:2017-08-03 02:26:29

标签: php mysql

我有来自数据库的多选字段。例如我在页面(storeform.php)中选择的文件:

<select name="store[]" multiple="multiple">
<option value=1>Outlet 1</option>
<option value=2>Outlet 2</option>
<option value=3>Outlet 3</option>
<option>.......</option>
</select>

我想使用php post从上面的select字段中获取值到db并将值放入Query.This是我在其他页面中写的(storetable.php):

<?php
$storenum=$_POST['store'];
foreach($storenum as $snumber)
{
    //this part i need to get the values and put the values into Query.
    //for example the $storenum hold values $1 and $2, so $1,$2 i need to put in Query.
}

$query="SELECT sum(a.netamt) as netamt, b.store_name,
  c.monusage,c.monusage/sum(a.netamt)*1000 as duh 
  FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
  JOIN site_salmonusage c ON b.storenum = c.storenum 
  WHERE c.month = '$date211' AND (a.storenum='$1' OR a.storenum='$2') AND a.busidate >= '$date1' AND a.busidate <='$date2'
  GROUP BY a.storenum order by duh"

/* code continue */

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。

<?php
$storenum=implode(",",$_POST['store']);


$query="SELECT sum(a.netamt) as netamt, b.store_name,
  c.monusage,c.monusage/sum(a.netamt)*1000 as duh 
  FROM site_sales a JOIN site_store b ON b.storenum = a.storenum 
  JOIN site_salmonusage c ON b.storenum = c.storenum 
  WHERE c.month = '$date211' AND a.storenum IN ($storenum) AND a.busidate >= '$date1' AND a.busidate <='$date2'
  GROUP BY a.storenum order by duh"