NiallFH Posted August 22, 2013 Share Posted August 22, 2013 Can anyone explain to me why this code updates the records fine, yet the code below it doesn't? To me, they are both the same in principle, yet one works and the other doesn't? Tearing my hair out! Example 1: <form method="post" action="<?php echo "$PHP_SELF?sessioid=$sessio&action=modify&id=$matchid" ?>"> <?php $get_goalscorers = mysql_query("SELECT CONCAT(tplss_players.PlayerFirstName, ' ', tplss_players.PlayerLastName) AS playername, tplss_goals.GoalMinute AS minute, tplss_goals.GoalID as id, tplss_goals.GoalPenalty AS pen, tplss_goals.GoalOwn AS own, tplss_goals.GoalOwnScorer AS ownscorer FROM tplss_players, tplss_goals WHERE tplss_players.PlayerID = tplss_goals.GoalPlayerID AND tplss_goals.GoalMatchID = '$matchid' AND tplss_goals.GoalSeasonID = '$seasonid' ORDER BY minute ", $connection) or die(mysql_error()); if(mysql_num_rows($get_goalscorers) == 0) { echo'No goals added.'; } else { while($gsdata = mysql_fetch_array($get_goalscorers)) { echo"<input type=\"hidden\" name=\"goal_id[]\" value=\"$gsdata[id]\">"; if($gsdata['own'] == 1) { echo"$gsdata[ownscorer] (o.g.) <input type=\"text\" name=\"minute[]\" size=\"2\" value=\"$gsdata[minute]\"><br>\n"; } elseif($gsdata['pen'] == 1) { echo"$gsdata[playername] (pen.) <input type=\"text\" name=\"minute[]\" size=\"2\" value=\"$gsdata[minute]\"><br>\n"; } else { echo"$gsdata[playername] <input type=\"text\" name=\"minute[]\" size=\"2\" value=\"$gsdata[minute]\"><br>\n"; } } } mysql_free_result($get_goalscorers); ?> <input type="submit" name="modify_goals" value="Modify goal times"> </form> elseif($modify_goals) { $goalid = $_POST['goal_id']; $goalminute = $_POST['minute']; foreach($goalid as $key=>$goaltime) { mysql_query(" UPDATE tplss_goals SET GoalMinute = '$goalminute[$key]' WHERE GoalID = '$goalid[$key]' ",$connection); } } Example #2 (doesn't update): <form method="post" action="<?php echo "$PHP_SELF?sessioid=$sessio&action=modify&id=$matchid" ?>"> <input type="hidden" name="matchid" value="<?php echo $matchid ?>"> <input type="hidden" name="season_id" value="<?php echo $seasonid ?>"> <?php $get_substitutions = mysql_query(" SELECT CONCAT(P.PlayerFirstName, ' ', P.PlayerLastName) AS playername, CONCAT(PL.PlayerFirstName, ' ', PL.PlayerLastName) AS playername2, S.SubstitutionMinute AS minute, S.SubstitutionID AS id FROM tplss_players P, tplss_players PL, tplss_substitutions S WHERE S.SubstitutionMatchID = '$matchid' AND S.SubstitutionSeasonID = '$seasonid' AND P.PlayerID = S.SubstitutionPlayerIDIn AND PL.PlayerID = S.SubstitutionPlayerIDOut ORDER BY minute ", $connection) or die(mysql_error()); if(mysql_num_rows($get_substitutions) == 0) { echo'No substitutions added.'; } else { while($subbydata = mysql_fetch_array($get_substitutions)) { echo"<input type=\"hidden\" name=\"subbyid[]\" value=\"$subbydata[id]\">"; echo"$subbydata[playername] for $subbydata[playername2] <input type=\"text\" name=\"minute[]\" size=\"2\" value=\"$subbydata[minute]\"><br>\n"; } } mysql_free_result($get_substitutions); ?> <input type="submit" name="modifysubs" value="Modify sub times"> </form> elseif($modifysubs) { $subbyid = $_POST['subbyid']; $subbyminute = $_POST['minute']; foreach($subbyid as $key=>$subbytime) { mysql_query(" UPDATE tplss_substitutions SET SubstitutionMinute = '$subbyminute[$key]' WHERE SubstitutionID = '$subbyid[$key]' ",$connection) or die(mysql_error()); } } Quote Link to comment https://forums.phpfreaks.com/topic/281468-foreach-updating-multiple-records-conundrum/ Share on other sites More sharing options...
mac_gyver Posted August 22, 2013 Share Posted August 22, 2013 and the other doesn't? what does it do and exactly at what point in the form/form processing program execution doesn't it work? is the 'view source' of the form correct? is the $modifysubs variable true so that the code branch even runs? what have you done to pin down where the problem is at (we don't have your complete code, your data, nor are we standing right next to you and cannot run your code nor did we see what you saw that leads you to believe the other code doesn't work.) Quote Link to comment https://forums.phpfreaks.com/topic/281468-foreach-updating-multiple-records-conundrum/#findComment-1446320 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.