而循环只插入最后记录

时间:2013-11-29 14:04:00

标签: php mysql

如果行符合条件,我有一个显示复选框的while循环。它是我想提交并执行插入的形式。这是循环的代码:

<form action'' method='post'>

$result = mysqli_query($con,"SELECT * FROM b_tasks_report WHERE TASK_ID=$taskid GROUP BY WEEK_ID");

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='noborder'><input type='checkbox' name='invoicename[]' value='$invoicename - Week " . $row['WEEK_ID'] . "'>Week " . $row['WEEK_ID'] . "</td>";
echo "</tr>";
}
echo "</table>";
<input type='submit' name='submit' value='submit'></form>

这当前返回三个复选框,每个复选框标记为第1周,第2周和第3周。我遇到的问题是,当我提交表单并执行插入时,它只插入最后一行。这是我的插入内容:

$sql="INSERT into b_sale_order (LID,PERSON_TYPE_ID,PAYED,CANCELED,STATUS_ID,DATE_STATUS,EMP_STATUS_ID,PRICE_DELIVERY,ALLOW_DELIVERY,DEDUCTED,MARKED,RESERVED,PRICE,CURRENCY,DISCOUNT_VALUE,USER_ID,PAY_SYSTEM_ID,DATE_INSERT,DATE_UPDATE,TAX_VALUE,SUM_PAID,RECOUNT_FLAG,UPDATED_1C,ORDER_TOPIC,RESPONSIBLE_ID,DATE_BILL)
VALUES
('s1','1','N','N','A','$_POST[insertdate]','1','0.00','N','N','N','Y','0.00','GBP','0.00','1','1','$_POST[insertdate]','$_POST[insertdate]','0.00','0.00','Y','N','$_POST[invoicename]','1','$_POST[insertdate]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }

我多次使用insertdate post项目。我尝试将以下内容添加到表单字段的名称中:

insertdate[] 

然后包括上面的插入a:

foreach($_POST['insertdate'] as

但我无法让它发挥作用。是因为我在不同的领域多次使用相同的帖子项目?我应该为其添加一个隐藏的ID字段吗?

总而言之,我只想根据勾选的复选框添加1-3个新行。

我们将不胜感激。提前致谢

1 个答案:

答案 0 :(得分:1)

首先,insert语句中的所有索引$ _POST变量都应该有引号,试试这个

$names= $_POST['invoicenames']
foreach($names as $invoicename){
$sql="INSERT into b_sale_order (LID,PERSON_TYPE_ID,PAYED,CANCELED,STATUS_ID,DATE_STATUS,EMP_STATUS_ID,PRICE_DELIVERY,ALLOW_DELIVERY,DEDUCTED,MARKED,RESERVED,PRICE,CURRENCY,DISCOUNT_VALUE,USER_ID,PAY_SYSTEM_ID,DATE_INSERT,DATE_UPDATE,TAX_VALUE,SUM_PAID,RECOUNT_FLAG,UPDATED_1C,ORDER_TOPIC,RESPONSIBLE_ID,DATE_BILL)
VALUES
('s1','1','N','N','A','".$_POST['insertdate']."','1','0.00','N','N','N','Y','0.00','GBP','0.00','1','1','".$_POST['insertdate']."','".$_POST['insertdate']."','0.00','0.00','Y','N','".$invoicename."','1','".$_POST['insertdate']."')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
 }