Reputation: 459
I have a while loop that displays checkboxes if a row meets the criteria. It is within a form which I would like to submit and perform an insert. This is the code for the loop:
<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>
This currently returns three checkboxes each labelled week 1, week 2 and week 3. The issue I'm having is that when I submit the form and perform the insert then it only inserts the last row. Here is my insert:
$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));
}
I use the insertdate post item a number of times. I have tried adding the following to the name of the form field:
insertdate[]
And then including above the insert a:
foreach($_POST['insertdate'] as
But I can't get it to work. Is it because I'm using the same post item a number of times for different fields? Should I add a hidden ID field for it to look at instead?
All in all I just want it to add 1-3 new rows depending on how many checkboxes are ticked.
Your help would be appreciated. Thanks in advance
Upvotes: 0
Views: 700
Reputation: 89
first of all indexes $_POST variables in the insert statement should have quotes, try this
$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));
}
}
Upvotes: 1