我再次陷入PHP,并拥有以下更新代码。这有效,但仅适用于插入的最后一个?我需要更新所有gritem id。我认为将[]添加到一个非唯一的表单字段允许PHP将其解析为一个数组,但它仍然将其视为一个字段?
我的HTML:
<input type="hidden" name="gritemid[]" value="<?PHP echo $row2['gritemid']; ?>">
<input type="text" name="grqty[]" id="grqty[]" value="" />
以下PHP代码
if(isset($_POST['storageloc']))
{
//Set the GRQTY to subtract from the Orderqty on each item in the list if selected
// find out how many records there are to update
$size = count($_POST['gritemid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$gritemid= $_POST['gritemid'][$i];
$grqty= $_POST['grqty'][$i];
if ($grqty > "0") {
// do the update and print out some info just to provide some visual feedback
$query = "UPDATE gr_items SET orderqty = orderqty - $grqty WHERE gritemid = '$gritemid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
}
++$i;
}
mysql_close();
答案 0 :(得分:0)
首先,在将它们放入查询之前,您确实应该对post vars进行一些验证/清理。由于您使用的变量似乎都是整数,我建议使用intval()
-
// define each variable
$gritemid = intval($_POST['gritemid'][$i]);
$grqty = intval($_POST['grqty'][$i]);
如果用print($query);
替换对mysql_query()的调用,你会看到你期望的多个查询吗?