Jump to content

[SOLVED] Handeling large forms of "unknown" size


SyncViews

Recommended Posts

How can I build a form (useing a sql query like the one below) that the user can use to update an entire table in one go (eg like the table at the bottom)?

 

$result = mysql_query("SELECT * FROM sections ORDER BY Display_ID DESC");
while ($data = mysql_fetch_array($result))
{
    the form. This can post the data to another page which actauly makes the change
}

The table "sections"

ID int NOT NULL AUTO_INCREMENT,
DisplayID int,
Name varchar(64),
Description text,
PRIMARY KEY(ID)

 

PHP: 5.2.5

mysql: 5.0.27

Host: http://x10hosting.com

It creates a really ugly form, but this would work for building the form (I haven't tested, so I can't guarantee there's no syntax errors):

 

$result = mysql_query("SELECT * FROM sections ORDER BY Display_ID DESC");

echo '<form method="post">';

while ($data = mysql_fetch_array($result)) {
    echo '<br />
      Name: <input name="row[' . $data['id'] . '][Name]" value="' . $data['Name'] . '"><br />
      Description: <textarea name="row[' . $data['id'] . '][Description]">' . $data['Description'] . '</textarea><br />';
    echo str_repeat("-", 20);
}

 

Then on the processing side:

 

foreach ($_POST['row'] as $DisplayID => $data) {
   $query = "UPDATE tablename SET Name = '" . $data['Name'] . "', Description = '" . $data['Description'] . "' WHERE DisplayID = " . $DisplayID;

   mysql_query($query);
}

 

This isn't a good solution because you will be updating each and every row each time the page is submitted.  So, if there are 1000 rows, and only 1 gets updated...that means 999 unnecessary updates.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.