homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Dynamic Form Generation Via PHP + MYSQL
php, webform, mysql, html

 2:12 am on Jun 13, 2009 (gmt 0)

Ok this is a issue i've been wrestling with for a few days now, and just for the life of me can not wrap my head around this.

What I am wanting to do is Pull records from the MYSQL DB and create a dynamic form showing those records + fields so a user can update them.

Now I can pull and place in the form with no problem. The problem comes in when saving the changes.

There could be 50+ records that are pulled in from the DB. So that would create

<input id="Name1"><input id="URL1">
<input id="Name2"><input id="URL2">

So obviously from record one, Name1, and URL1 are from that record.

So how can I dynamically account for a ever changing list of veriables that come in and keep all the fields linked to the right record. Meaning so I don't have URL2 being updated for record 1 and so forth.

Since this is one huge form, i'm at a loss.

I'm wanting to do it this way, because before I had each record in it's own form. Which as you could guess required a Submit button for each field. And my users were a little annoyed with that setup. So one form so they can change all data that needs changed and update it all at once would be great.

Any assistance would be greatly appreciated.

Thank you.



 6:08 am on Jun 13, 2009 (gmt 0)

Welcome aboard Alzeric, here's the logic (not working code:)

select id,name,url from table;
... while . . .

$namefield = 'name_' . $row['id'];
$urlfield = 'url_' . $row['id'];
$name = $row['name'];
$url= $row['url'];
<input type="text" id="$namefield" name="$namefield" value="$name">
<input type="text" id="$urlfield" name="$urlfield" value="$url">

then on submit . . .

foreach ($_POST as $key=>$value) {
if (preg_match('/name\_\d+/',$key) {
($tag,$id) = explode('_',$key);
$urlfield = 'url_' . $id;
$select = "update table set name='".$value."',url='".$_POST[$urlfield']."' where id=$id";


 7:10 am on Jun 13, 2009 (gmt 0)

Here's some code you can have a play with which actually gets the mysql column names from a table (could have 5, could have 50) and checks if the $_POST value is present. This is bare bones code, so please use it for testing only.

$query = <<<SQL
SHOW COLUMNS FROM `tablenamehere`

$res = mysql_query($query);
while($row = mysql_fetch_object($res))

if(array_key_exists($row->Field,$_POST)) echo $row->Field." Exists";
echo "<p>$row->Field</p>";
echo "<hr />";


You could also look up the 'active record' design pattern which sounds like it may help in this case


Systems like CodeIgniter have excellent built in functionality for this kind of thing. You can just submit a ($_POST) array and it updates all the fields


 8:39 am on Jun 13, 2009 (gmt 0)

great work guys i'll give it a run through tomorrow when i wake.

thank you for the shove in the right direction.


 5:12 pm on Jun 13, 2009 (gmt 0)

What rocknbil said but i'd suggest you use arrays instead. Something like this to create the form elements:

while($row = mysql_fetch_array($qh)) {
$id = $row['id'];
echo '<input type="hidden" name="id[]" value="'.$id.'">';
echo '<input type="text" name="name['.$id.']" value="'.htmlentities($row['name']).'">';
echo '<input type="text" name="url['.$id.']" value="'.htmlentities($row['url']).'">';

And smething like this to process the submit:

foreach($_POST['id'] as $id) {
$name = $_POST['name'][$id];
$url = $_POST['url'][$id];


 12:37 am on Jun 14, 2009 (gmt 0)

Ok had time to play around with each of your codes.

In the end I went with rocknbil's suggestion. I ended up just exploding the variable names and parsing by ID #.

@idfer you suggestion is close to rocknbil's however that was what I was doing in the beginning and it wouldn't work because there was no way to link the hidden field directly to a particular line in the form. Considering each entry was in the same form. You method would worked fantastically if I was dealing with a single record.

Thank you all for your help and assistance, it was invaluable all of you. Since each one of you provided different approaches to the issue. And in the future I can use each one of them for different purposes.


 3:41 am on Jun 14, 2009 (gmt 0)

it wouldn't work because there was no way to link the hidden field directly to a particular line in the form.

Sure it works, i do it all the time myself, heheh. The hidden fields just tell you what record IDs are in the form, and the other fields are indexed by those record IDs. It's pretty much the same thing as rocknbil's solution except you use [$id] instead of _$id when creating the field names, and it saves you the trouble of exploding those names later.

Anyways, as long as you got working it's all good. :-)


 6:12 am on Jun 14, 2009 (gmt 0)

Just an aside, idfer's method is probably the "correct" and "PHP-ish" way to approach the problem, but I do it this way because when you have 5,000-20,000 lines of dynamic code it's so much easier to search for and spot the "identifiers" if you set explicit names on them.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved