Jump to content

Check box insert with other form items


ShoeLace1291

Recommended Posts

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

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. :-[

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

LOL ;D 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. 8)

 

Basically I just repeated what timmah1 already say. :-X

 

Here is link on simillar post with my response :D:

http://www.phpfreaks.com/forums/index.php/topic,237610.msg1107245.html#msg1107245 ;)

 

 

Archived

This topic is now archived and is closed to further replies.

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