Jump to content

[SOLVED] updating multiple values in database...


galvin

Recommended Posts

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.

 

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

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.