PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
that should read I haven't been able to do this.
I believe I need to get this into an array so I can then send that data to my DB.
There maybe 1 - 10 iterations of this array.
thanks for taking the time to look into this. (requested code is below)
I really only know how to explain the business logic so I hope you don't mind that..
The user fills out the initial row. Then clicks the add link and a new row appears, and so on.
Now once it's submitted I need to go through each of these rows and insert into mysql. I can do one, I just don't know how to set up this loop.
I do know that each rows needs a unique identifier for a loop to iterate through it.
That is because the $ProductID variable is completely nonexistent in the scope of the function. Of course, you are not seeing that issue with that because the error suppression operator (@) is quieting the output of the warning for undefined/uninitialized variable. Now, on to your logic...
I assume that the form you are talking, in which a client adds rows, is handling the row adds through javascript. Is that correct? If it is, what you need to do is set each field in each row to an array (name="fieldname[]") instead of a string (name="fieldname"). Then you can loop through each of the array members to handle your inputs once the form is submitted for processing.
Thanks for info about the "error suppression operator". I wasn't aware of that.
To simplify things if statically limit data entry to 10 rows how could I insert the data into the db consecutively?
This code works but only for the last row.
$sql_add_invoice_items = "INSERT INTO invoice_items VALUES('','$prod_name[1]', '$quantityID[1]')";
mysql_query($sql_add_invoice_items) or die(mysql_error());
You'd loop the array and at each iteration you'd process the SQL (unless your server version supports transactions, then you could just throw one big query at the database and rollback if there is an error).
Each row of the form would need to have a counter. This makes it simpler in your loop. As you add a row to the form, add 1 to the row counter, so in your form you'd have fields similar to:
<?php
$count = count($_POST['rowcount']);
$prods = $_POST['productId'];
$quan = $_POST['quantityId'];
for ($i = 0; $i < $count; $i++)
{
$sql = "INSERT INTO `invoice_items` VALUES('','{$prods[$i]}', '{$quan[$i]}'";
// yadda yadda
}
?>
Please note: this code is only an example. It will need to be reworked, but it might be the logic you are looking for, or at least along the same lines.