Jump to content
NiallAA

Checking if a checkbox is checked inside a foreach loop

Recommended Posts

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.

Edited by NiallAA

Share this post


Link to post
Share on other sites

You are using obsolete code that has been removed from Php. You need to use PDO with prepared statements. You are also vulnerable to an SQL Injection Attack. You never ever use variables in your query. The entire thing needs to be tossed.

 

https://phpdelusions.net/pdo

Edited by benanamen

Share this post


Link to post
Share on other sites

 

if(isset($opp_played['$key']))

 

You have the variable inside single quotes - remove them. (The value it is looking for is the literal string $-k-e-y instead of the variable's value)

Share this post


Link to post
Share on other sites

since only checked checkboxes are submitted, there will be no guaranteed relationship between the submitted opp_id[] and opp_played[] values. upon each un-checked checkbox, the id you are getting from the opp_id[] data will be OFF from the checkbox it is for. you could sequentially number both the opp_id[...] and opp_played[...] using code, but there's no need for the hidden opp_id field at all. read on.

 

perhaps you should use a pair of radio buttons for each player instead? this will submit a value for each player, so that you don't have to deal with checked to un-checked checkboxes that won't be in the submitted data.

 

the checkboxes (or radio buttons) need to contain the player id values, as the array index.

Edited by mac_gyver

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.