Hi folks,
Trying to run a MySQL UPDATE statement on a small table which basically has three fields. AppID, AppMatchID and AppPlayed.
I have a form which sends two values via POST, which are app_played and matchid. app_played is a checkbox value.
I am trying to update multiple rows at once using a submit button in the form, but coming into problems where it concerns where to check isset in the foreach loop.
Here is my form:
<form method="post" action="matchdata.php">
<input type="hidden" name="matchid" value="<?php echo $matchid ?>">
<?php
// Get former players involved in opposition team
$get_former_players = mysql_query("
SELECT L.OppID AS id, CONCAT(P.PlayerFirstName, ' ', P.PlayerLastName) AS name, L.OppPlayed AS played FROM db_opp_players L, tplss_players P WHERE L.OppMatchID = $matchid AND L.OppPlayerID = P.PlayerID ORDER BY P.PlayerLastName ASC, P.PlayerFirstName ASC",$connection) or die(mysql_error);
while($opp_player_data = mysql_fetch_array($get_former_players))
{
echo"
<input type=\"hidden\" name=\"opp_id[]\" value=\"$opp_player_data[id]\">
$opp_player_data[name]
";
if($opp_player_data['played'] == 1)
echo"<input type=\"checkbox\" name=\"opp_played[]\" value=\"1\" CHECKED>";
else
echo"<input type=\"checkbox\" name=\"opp_played[]\" value=\"1\">";
echo"Played?";
echo"
<a href=\"matchdata.php?action=remove_from_opp_players&id=$opp_player_data[id]&matchid=$matchid\">
<img src=\"images/remove.gif\" border=\"0\" ALT=\"Remove\"></a>
<br>
";
}
?>
<input type="submit" name="modify_opp_players" value="Update">
<?
mysql_free_result($get_former_players);
?>
</form>
And here is my update command, after receiving POST information from the submit button.
$opp_id = $_POST['opp_id'];
$matchid = $_POST['matchid'];
$opp_played = $_POST['opp_played'];
foreach($opp_id as $key=>$played)
{
if(isset($opp_played['$key']))
{
mysql_query("
UPDATE
db_opp_players
SET
OppPlayed = '1'
WHERE
OppID = '$opp_id[$key]'
",$connection) or die(mysql_error());
}
else
{
mysql_query("
UPDATE
db_opp_players
SET
OppPlayed = '0'
WHERE
OppID = '$opp_id[$key]'
",$connection) or die(mysql_error());
}
}
Can anyone offer me a simpler solution, or better still something that works? At the moment the checkboxes either don't update, or only update the first entry.