ShoeLace1291 Posted February 6, 2009 Share Posted February 6, 2009 I want to use checkboxes to insert a new database row. A tutorial I got says to use a foreach($_POST as $id) to loop through the checked boxes. My form, however, has items other than checkboxes so how would I loop through only checkboxes? This is my code: if($_POST['submit']){ $query = mysql_query("UPDATE fc_matches SET dateplayed = '".$_POST['dateplayed']."', ourscore = '".$_POST['ourscore']."', theirscore = '".$_POST['theirscore']."', lastmodified = '".date("o-m-d")."' WHERE matchID = '".$_GET['id']."'") or die("Error: ".mysql_error()); foreach($_POST as $id){ $insert = mysql_query("INSERT INTO match_players(memberID,matchID) values('".$id."', '".$_GET['id']."')") or die("Error: ".mysql_error()); } header("Location:matches.php?view=match;id=".$_GET['id']); } else { $query = mysql_query("SELECT * FROM fc_matches WHERE matchID = '".$_GET['id']."'") or die("Error: ".mysql_error()); $match=mysql_fetch_array($query); $query2 = mysql_query("SELECT * FROM smf_members WHERE ID_GROUP = 25 OR ID_GROUP = 29 OR ID_GROUP = 1 ORDER BY memberName") or die("Error: ".mysql_error()); echo "<table align='center' cellspacing='1' cellpadding='4' border='0'> <tr><form action='".$_SERVER['PHP_SELF']."' method='POST'> <td align='right'>Opponent: </td> <td align='left'>".$match["opponent"]."</td> </tr><tr> <td align='right'>Opponent Score: </td> <td align='left'><input type='text' name='theirscore' size='30'></td> </tr><tr> <td align='right'>[4thC] Score: </td> <td align='left'><input type='text' name='ourscore' size='30'></td> </tr><tr> <td align='right' style='vertical-align: top;'>Match Players: <br><small>Select the members of our clan that played in the match.</small></td> <td>"; while($player=mysql_fetch_array($query2)){ echo $player["memberName"]."<input type='checkbox' value='".$player["ID_MEMBER"]."' name='".$player["ID_MEMBER"]."' id='".$player["ID_MEMBER"]."'><br>"; } echo "</td> </tr><tr> <td align='center' colspan='2'><input type='submit' name='submit' value='Report'></td></form> </tr> </table>"; } Link to comment https://forums.phpfreaks.com/topic/144025-check-box-insert-with-other-form-items/ Share on other sites More sharing options...
anthylon Posted February 6, 2009 Share Posted February 6, 2009 Hmm, I would use different naming for checkboxes. Name all checkboxes like: chk_name. Than you could loop searching for POST variable with name chk_ - something like that. What you use - naming objects by player_id is not necessary. Try it - it should work. :-\ Good luck. Link to comment https://forums.phpfreaks.com/topic/144025-check-box-insert-with-other-form-items/#findComment-755730 Share on other sites More sharing options...
ShoeLace1291 Posted February 9, 2009 Author Share Posted February 9, 2009 Now I get an invalid request variable error when I submit the form. Just a white page with "Invalid request variable." on it. Link to comment https://forums.phpfreaks.com/topic/144025-check-box-insert-with-other-form-items/#findComment-757829 Share on other sites More sharing options...
timmah1 Posted February 9, 2009 Share Posted February 9, 2009 echo $player["memberName"]."<input type='checkbox' value='".$player["ID_MEMBER"]."' name='"playerID[]"' id='".$player["ID_MEMBER"]."'><br>"; Then for the insert $player = $_POST['playerID']; $count = count($player); for ($i = 0; $i < $count ; $i++) { $playerID = $player[$i]; $insert = mysql_query("INSERT INTO match_players(memberID,matchID) values('".$playerID."', '".$_GET['id']."')") or die("Error: ".mysql_error()); } Not tested, but it's the general idea of what you want Link to comment https://forums.phpfreaks.com/topic/144025-check-box-insert-with-other-form-items/#findComment-757839 Share on other sites More sharing options...
anthylon Posted February 9, 2009 Share Posted February 9, 2009 LOL got one bid related to simillar issue and than I realised my way would be wrong :-\. I already answered again but on different - simillar post - about the same issue. Yes, do what timmah1 - it will work 100% . In this case - which is right one - you sending multiple checkbox selected on the form as array. What you need to do once when you get array is easy: enumerate it and you'll get all values. Basically I just repeated what timmah1 already say. :-X Here is link on simillar post with my response : http://www.phpfreaks.com/forums/index.php/topic,237610.msg1107245.html#msg1107245 Link to comment https://forums.phpfreaks.com/topic/144025-check-box-insert-with-other-form-items/#findComment-757902 Share on other sites More sharing options...
ShoeLace1291 Posted February 9, 2009 Author Share Posted February 9, 2009 Thanks, timmah, that helped a bunch! Works flawlessly! Link to comment https://forums.phpfreaks.com/topic/144025-check-box-insert-with-other-form-items/#findComment-757997 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.