# Solved: PHP/MySQL: Storing dynamically generated forms in a list to a mysql table.



## aasmith26 (Nov 28, 2008)

*OK Folks, I need your help with this one. *

I have the understanding of creating the dynamically generated HTML table, so many input boxes per row (a set number of input boxes.)

Of course in the MySQL table I have the set number of fields per row. Example:

RecID - Uniquely Generated
Bookname
BookISBN
Price

+------------+----------+-----------+-----+----+
| BookName | BookISBN | Price | Add |Del |
+------------+----------+-----------+-----+----+
| txtBkName1 | txtISBN1 | txtPrice1 | btn |btn |
+------------+----------+-----------+-----+----+
{these will add new rows each time the btnAdd button
is clicked.} (txtBkName2, etc and will increase)

Now, i'll need to update the MySQL table with this information using some sort
of loop that will increase until the total number of rows is reached.

Any advice that ya'll can offer would be great.

Thank you.


----------



## aasmith26 (Nov 28, 2008)

Figured it out on my own. Excellence. Here is the code if someone happens to stumble upon this forum.

This is just an example.


```
$database="test";
@mysql_connect("localhost","user","password") or die("Unable to connect.");
@mysql_select_db($database);

$a=1;
$numrows=$_POST['numrows'];
$ordernumber=$_POST['ordernumber'];
echo "<table border=1>";
while($a<=$numrows) {
$data = "txtField" . $a;
$textfield=$_POST[$data];
echo "<td>Processed Item $a<tr><td>";
echo "Data for field $a is $textfield";
echo "<tr>";
$query="INSERT INTO data VALUES(0,'$ordernumber','$textfield','Blah')";
mysql_query($query) or die(mysql_error());

$a++;  /* add 1 more to the loop until the loop is done. */

}
```


----------



## ehymel (Aug 12, 2007)

There are lots of ways to do this, and there is no "right" answer about how to do this. Having said that, a "for loop" would be easier to follow in my opinion. See what you think. I also tried to clean up the table and make the generated html code a little easier to read.


```
$database="test";
@mysql_connect("localhost","user","password") or die("Unable to connect.");
@mysql_select_db($database);

$a=1;
$numrows=$_POST['numrows'];
$ordernumber=$_POST['ordernumber'];
echo "[TABLE]\n";
for ($a=1; $a<=$numrows; $a++) {
   $data = "txtField" . $a;
   $textfield=$_POST[$data];
   echo "[TR][TD]Processed Item $a[/TD]\n";
   echo "[TD]Data for field $a is $textfield[/TD]\n";
   echo "[/TR]\n";
   $query="INSERT INTO data VALUES(0,'$ordernumber','$textfield','Blah')";
   mysql_query($query) or die(mysql_error());
}
echo "[/TABLE]";
```


----------



## aasmith26 (Nov 28, 2008)

ehymel said:


> There are lots of ways to do this, and there is no "right" answer about how to do this. Having said that, a "for loop" would be easier to follow in my opinion. See what you think. I also tried to clean up the table and make the generated html code a little easier to read.
> 
> 
> ```
> ...


Thanks! Ill implement the code! After I wrote this I experienced extreme difficulty generating dynamic rows in HTML and JavaScript. I figured it out. I really wish there was an easier way though. I'm on my blackberry now ill see if I can't get the HTML and JS code up later!! Thx again!!!


----------



## ehymel (Aug 12, 2007)

Good luck... don't forget to mark your thread as solved.


----------

