galvin Posted January 16, 2009 Share Posted January 16, 2009 I have the following table that lists multiple running backs names and has a simple text field to the right of each name that allows me to enter a number (i.e. rank) next to each name ... $sql = "SELECT firstname, lastname, playerid FROM runningbacks"; $RBs_set = mysql_query($sql, $connection); if (!$RBs_set) { die("Database query failed: " . mysql_error()); } else { while ($RBsArray = mysql_fetch_array($RBs_set)) { echo "<tr><td>{$RBsArray['firstname']} {$RBsArray['lastname']}</td><td><input type='hidden' class='' size='5' name='playerid' value='{$RBsArray['playerid']}'><input type='text' class='' size='5' name='rank' value=''></td></tr>"; } echo "<tr><td><input type='submit' name='submit' value='Update Runningbacks'></td></tr>"; } ...and then I submit the page to the following code to simply update the rank (in a mysql table) from what it was to the new rank I entered above... if (isset($_POST['submit']) && $_POST['submit'] == "Update Runningbacks" ) { $sql = "UPDATE runningbacks SET `rank` = '{$_POST['rank']}' WHERE `playerid` = '{$_POST['playerid']}'"; $updaterbrank = mysql_query($sql, $connection); if (!$updaterbrank) { die("Database query failed: " . mysql_error()); } else { $updated="yes, they updated."; } } ...but this mysql query is ONLY updating the rank of the player with playerid = "7" (NOTE: I only have 7 players in the table right now). Any ideas why it's only updating ONE and not all SEVEN? I assumed it would do update the rank for any player with a rank entered in the form listed at the beginning of the post, but maybe I'm missing something where maybe I have to loop through them or something? Any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/141038-solved-updating-multiple-values-in-database/ Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 It is because you have all of the inputs named as playerid and rank respectively. If you look at your source code, I bet that you will find this. So by that reasoning, when you submit the code, $_POST['playerid'] ends up being set to the value of the last input box with that name in the form. Same goes for the rank input box. Either give them unique names, or make them arrays and loop through them. You can make them arrays by adding [] to the end of the name e.g. <input name="playerid[]" value=""> Thats what I see on quick observation. Nate Link to comment https://forums.phpfreaks.com/topic/141038-solved-updating-multiple-values-in-database/#findComment-738200 Share on other sites More sharing options...
galvin Posted January 16, 2009 Author Share Posted January 16, 2009 That makes sense. I think that definitely explains it. I'll give it an array name and mess with that. Thanks a lot, chronister! Link to comment https://forums.phpfreaks.com/topic/141038-solved-updating-multiple-values-in-database/#findComment-738339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.