NiallAA Posted January 30, 2017 Share Posted January 30, 2017 (edited) 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 January 30, 2017 by NiallAA Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 30, 2017 Share Posted January 30, 2017 (edited) 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 January 30, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
Barand Posted January 30, 2017 Share Posted January 30, 2017 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) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 30, 2017 Share Posted January 30, 2017 (edited) 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 January 30, 2017 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.