In a nutshell, I'm looping and trying to INSERT all the rows but it only inserts the last row for as many times that it loops. So, for two loops, it will loop through twice, and insert the last row twice (not inserting the first row at all).
I'm using this as the row / items counter:
$countRecords;
This is a basic counter that tells me how many rows are included in the previous invoice. This works. In my example, it displays "2" as two items (rows) are in the previous invoice.
I'm using the first part of the MySQL outside of the loop:
// the MySQL statement out of the loop
$stmt="INSERT INTO o70vm_invoices_items
(`id`, `invoice_id`, `name`, `desc`, `value`, `amount`, `discount`, `ordering` ) VALUES ";
I'm then running the loop:
// loop through the rows
for($rowCounter = 0; $rowCounter < $countRecords; $rowCounter++)
{
// assign variables based on each loop iteration
$invoiceID = htmlentities($_POST['invoice_id'], ENT_QUOTES);
$program = htmlentities($_POST['name'], ENT_QUOTES);
$forWeek = htmlentities($_POST['desc'], ENT_QUOTES);
$value = htmlentities($_POST['value'], ENT_QUOTES);
$qty = htmlentities($_POST['amount'], ENT_QUOTES);
// put the VALUES array string for each row as such
$ValuesAddToQuery[] ="(NULL, '$invoiceID', '$program', '$forWeek', '$value', '$qty','0.00','1')";
}
After the loop, I implode the $ValuesAddToQuery array to insert as such:
// add all the values into the statement
$stmt .= implode(',',$ValuesAddToQuery);
echo $stmt;
// execute statement
//mysql_query($stmt) or exit(mysql_error());
As I said, this loop only inserts the last row the same number of times as it loops through the code.
My var_dump shows that I have the last row added twice in the array also.
So much appreciate any help on this.
For an uncut version of my loop, here it is:
$stmt="INSERT INTO o70vm_invoices_items
(`id`, `invoice_id`, `name`, `desc`, `value`, `amount`,`discount`,`ordering` ) VALUES ";
// loop through the rows
for($rowCounter = 0; $rowCounter < $countRecords; $rowCounter++)
{
// assign variables based on each loop iteration
$invoiceID = htmlentities($_POST['invoice_id'], ENT_QUOTES);
$program = htmlentities($_POST['name'], ENT_QUOTES);
$forWeek = htmlentities($_POST['desc'], ENT_QUOTES);
$value = htmlentities($_POST['value'], ENT_QUOTES);
$qty = htmlentities($_POST['amount'], ENT_QUOTES);
// put the VALUES array string for each row as such
$ValuesAddToQuery[] ="(NULL, '$invoiceID', '$program', '$forWeek', '$value', '$qty','0.00','1')";
}
// add all the values into the statement
$stmt .= implode(',',$ValuesAddToQuery);
echo $stmt."<br /><br />";
var_dump($ValuesAddToQuery);
// execute statement
//mysql_query($stmt) or exit(mysql_error());
For a little more detail, I'm getting the data from the previous invoice here:
<td>
<input type="number" title="'.$row->invoice_id_on_items.'" name="invoice_id" size="2" id="invoice_id" value="' . $row->invoice_id_on_items. '" />
</td>
<td>(<a href="getIndItems.php?id=' .$row->item_id. '" target="_blank">EDIT</a>)<br>'.$row->item_id.'</td>';
$_SESSION['itemIDSelected']=$row->item_id;
echo'
<input type="hidden" title="'.$row->item_id.'" name="id" size="13" id="id" value="'.$row->item_id. '" />
<td>
<input type="text" title="'.$row->forweek.'" name="desc" size="15" id="desc" value="' . $row->forweek. '" />
</td>
<td>
<input type="text" title="'.$row->program.'" name="name" size="50" id="name" value="' . $row->program. '" />
</td>
<td>
<input type="number" title="'.$row->fee.'" name="value" size="3" id="value" value="' . $row->fee. '" />
</td>
<td>
<input type="number" title="'.$row->qty.'" name="amount" size="3" id="amount" value="' . $row->qty. '" />
</td>
';
$Fee = floatval($row->fee);
$Qty = floatval($row->qty);
$ItemFee=$Fee*$Qty;
echo '
<td>
<input type="text" title="'.$ItemFee.'" name="total_fee" size="3" id="total_fee" value="' . $ItemFee. '" />
</td>
Then, I'm using the already illustrated above loop code to get the data. I am able to retrieve the past invoice data without issue but wanted to include it in my question so you would have all the necessary information to provide guidance.
// add all the values into the statement $stmt .= implode(',',$ValuesAddToQuery); echo $stmt; // execute statement //mysql_query($stmt) or exit(mysql_error());put these lines inside your loop