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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.