SyncViews Posted December 7, 2007 Share Posted December 7, 2007 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 Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 8, 2007 Share Posted December 8, 2007 you could delete the old table and create a new table with the same table name and the same row/field names. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 8, 2007 Share Posted December 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 8, 2007 Author Share Posted December 8, 2007 Thanks Seeing as it's for an admin panel so isn't goign to be used all that much I don't think it really matters if it updates stuff it doesn't have to. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.