Brandon Jaeger Posted September 7, 2008 Share Posted September 7, 2008 I want to generate the same form inputs for everyone in the database and have one Submit button and have it update them all at once. Am I on the right track? $query = "SELECT * FROM table"; $result = mysql_query($query) or die(mysql_error)); echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"POST\">\n"; $something_array1 = array(); $something_array2 = array(); $something_array3 = array(); $ids = array(); while($row = mysql_fetch_array($result)) { $something1 = $row['something1']; $something2 = $row['something2']; $something3 = $row['something3']; $id = $row['id']; echo "<input type=\"text\" name=\"something_array_1[]\" value=" . $something1 . "><br \>\n"; echo "<input type=\"text\" name=\"something_array_2[]\" value=" . $something2 . "><br \>\n"; echo "<input type=\"text\" name=\"something_array_3[]\" value=" . $something3 . "><br \>\n"; echo "<input type=\"hidden\" name=\"ids[]\" value=\"" . $id . "\">\n"; } echo "<input type=\"submit\" name=\"Update\" value=\"Update\">\n"; echo "</form>\n"; if(isset($_POST['Update'])) { $something_array1 = $_POST['something_array_1']; $something_array2 = $_POST['something_array_2']; $something_array3 = $_POST['something_array_3']; foreach($something_array1 as $val) { $result = mysql_query("UPDATE table SET column1 = " . $val . " WHERE id = " . $id); } foreach($something_array2 as $val) { $result = mysql_query("UPDATE table SET column2 = " . $val . " WHERE id = " . $id); } foreach($something_array3 as $val) { $result = mysql_query("UPDATE table SET column3 = " . $val . " WHERE id = " . $id); } } That's totally off the top of my head but I'm not sure if I'm heading the right way. If someone could either verify my method or correct me, I would greatly appreciate it! Thanks. Link to comment https://forums.phpfreaks.com/topic/123175-solved-updating-multiple-forms-at-once/ Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Almost correct, but you need to somehow get the ID into the foreach loops. I'd suggest echoing the ID as the array key (in the form name), and just adjusting the foreach accordingly. P.S: It's fine to PM me, that's why I have it in my sig. Don't worry about it. =) Link to comment https://forums.phpfreaks.com/topic/123175-solved-updating-multiple-forms-at-once/#findComment-636183 Share on other sites More sharing options...
Brandon Jaeger Posted September 7, 2008 Author Share Posted September 7, 2008 How would I do that? I'm not the best with PHP arrays. I primarily code Small ;x Link to comment https://forums.phpfreaks.com/topic/123175-solved-updating-multiple-forms-at-once/#findComment-636184 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 echo "<input type=\"text\" name=\"something_array_1[$id]\" value=" . $something1 . "><br \>\n"; For example. xD Then, you could change your foreach's to: foreach ($_POST['something_array_1'] AS $id => $value) { } Link to comment https://forums.phpfreaks.com/topic/123175-solved-updating-multiple-forms-at-once/#findComment-636185 Share on other sites More sharing options...
Brandon Jaeger Posted September 8, 2008 Author Share Posted September 8, 2008 I lub you <3 Link to comment https://forums.phpfreaks.com/topic/123175-solved-updating-multiple-forms-at-once/#findComment-636218 Share on other sites More sharing options...
DarkWater Posted September 8, 2008 Share Posted September 8, 2008 Lol, glad to help. xD Link to comment https://forums.phpfreaks.com/topic/123175-solved-updating-multiple-forms-at-once/#findComment-636221 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.