我在通过POST表单向数据库添加多行时遇到问题。以下是表格的简短摘录:
<form action='/process.php' 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))
{
$current_weekly_id = $row['WEEK_ID'];
if ($current_weekly_id == 1){
echo "<tr>";
echo "<td class='noborder'>
<input type='hidden' name='productname' value='Week 1 Hours'>
<input type='hidden' name='code' value='232'>
<input type='checkbox' name='wid[]" . $row['WEEK_ID'] . "' value='" . $row['WEEK_ID'] . "'>Week " . $row['WEEK_ID'] . "</td>";
echo "</tr>";
}
if ($current_weekly_id == 2){
echo "<tr>";
echo "<td class='noborder'>
<input type='hidden' name='productname' value='Week 2 Hours'>
<input type='hidden' name='code' value='232'>
<input type='checkbox' name='wid[]" . $row['WEEK_ID'] . "' value='" . $row['WEEK_ID'] . "'>Week " . $row['WEEK_ID'] . "</td>";
echo "</tr>";
}
<input style='margin-top: 15px;' type='image' name='submit'>
我想确保将这两行添加到数据库中。这是我的插页:
$sql="INSERT into b_sale_basket ()
VALUES
('".$_POST['productname']."','".$_POST['code']."','".$_POST['wid']."')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
这确实添加了最后一行但不是第一行。我知道我需要一个foreach但我不能让它工作。我坚持:
foreach($ _ POST ['wid'] as $ weekid
插入之前没有运气!如何让它添加两行呢?谢谢, 安迪