joebudden Posted January 12, 2007 Share Posted January 12, 2007 Hi, am writing a script to allow users to select check boxes then insert the data into a new tableHere is my code :[u]players.php:[/u][code]<?phpsession_start(); include("connect.php");//query to determine wether table is empty$query = "SELECT id, name, surname, ability, fitness, skill, position, prefmove FROM player order by surname";$result = mysql_query($query);$id = "id";$name = "name";$surname = "surname";$ability = "ability";$fitness = "fitness";$skill = "skill";$position = "position";$prefmove = "prefmove";//query to select total of players $query2 = "select count(id) as total from player"; $totalplayers = mysql_query($query2);$a = mysql_fetch_array($totalplayers, MYSQL_ASSOC);$total = $a['total'];if(isset($_POST["add_players"])){ foreach ($_POST as $key => $value) { //if ($value !== "Play Match") //{ $addsql = "insert into team (id , team) values ($value, 'user')"; mysql_query($addsql); echo $addsql; //} }}else{echo 'No player chosen';}require("players.view.php");?>[/code][u]players.view.php:[/u][code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>PHP Football Manager</title><link rel=stylesheet href="style.css" type="text/css"></head><body><table cellpadding="0" cellspacing="0"><tr><td><div class="title"><a href="players.php">Refresh </a><b>|</b><?phpecho $total;echo ' Players Found';?></div></td></tr><tr><td><br /><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><input type="submit" value="Play Match" /><br /><table cellpadding="1" cellspacing="0"><tr><td></td><td width ="30px" align="left">Id</td><td width ="75px" align="left">Name</td><td width ="130px" align="left">Surname</td><td align="center">Ability</td><td align="center">Fitness</td><td align="center">Skill</td><td align="center">Position</td><td align="left">Preferred Move</td></tr><?phpif (mysql_num_rows($result) !== 0){while($row = mysql_fetch_assoc($result)){echo '<tr class="rows"><td align="left">';echo '<input type="checkbox" name="add_players" value="'.$row[$id].'" /></td>';echo '<td align="left">'.$row[$id].'</td>';echo '<td align="left">'.$row[$name].'</td>';echo '<td align="left">'.$row[$surname].'</td>';echo '<td align="center">'.$row[$ability].'</td>';echo '<td align="center">'.$row[$fitness].'</td>';echo '<td align="center">'.$row[$skill].'</td>';echo '<td align="center">'.$row[$position].'</td>';echo '<td align="left">'.$row[$prefmove].'</td>';}}?></form></table></td></tr></table></body></html>[/code]The script works, but only inserts the first check box valueAnd i have tried a few things but always get the same resultAny ideas ??? Link to comment https://forums.phpfreaks.com/topic/33964-inserting-multiple-values-into-table-via-check-boxes/ Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 Name the checkboxes like this, note the "[]"[code]<?phpecho '<input type="checkbox" name="add_players[]" value="'.$row[$id].'" /></td>';?>[/code]then[code]foreach ($_POST['add_players'] as $value) { // insert $value into table}?>[/code] Link to comment https://forums.phpfreaks.com/topic/33964-inserting-multiple-values-into-table-via-check-boxes/#findComment-159587 Share on other sites More sharing options...
joebudden Posted January 12, 2007 Author Share Posted January 12, 2007 hey cheers Barand that worked , but for some reason now it has stopped working.can u spot any errors in my code ???it was working sweet a minute ago i dont know whats happened !!![code]<?phpsession_start(); include("connect.php");//query to determine wether player table is empty$query = "SELECT id, name, surname, ability, fitness, skill, position, prefmove FROM player order by surname";$result = mysql_query($query);$id = "id";$name = "name";$surname = "surname";$ability = "ability";$fitness = "fitness";$skill = "skill";$position = "position";$prefmove = "prefmove";//query to select total of players $query2 = "select count(id) as total from player"; $totalplayers = mysql_query($query2);$a = mysql_fetch_array($totalplayers, MYSQL_ASSOC);$total = $a['total'];if(isset($_POST["add_players"])){ foreach ($_POST["add_players"] as $value) { //if ($value !== "Play Match") //{ $addsql = "insert into team (id , team) values ($value, 'user')"; mysql_query($addsql); //} }}else{echo 'No player chosen';}require("players.view.php");?>[/code][code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>PHP Football Manager</title><link rel=stylesheet href="style.css" type="text/css"></head><body><table cellpadding="0" cellspacing="0"><tr><td><div class="title"><a href="players.php">Refresh </a><b>|</b><?phpecho $total;echo ' Players Found';?></div></td></tr><tr><td><br /><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><input type="submit" value="Add Players" /><br /><br /><br /><br /><table cellpadding="1" cellspacing="0"><tr><td></td><td width ="30px" align="left">Id</td><td width ="75px" align="left">Name</td><td width ="130px" align="left">Surname</td><td align="center">Ability</td><td align="center">Fitness</td><td align="center">Skill</td><td align="center">Position</td><td align="left">Preferred Move</td></tr><?phpif (mysql_num_rows($result) !== 0){while($row = mysql_fetch_assoc($result)){echo '<tr class="rows"><td align="left">';echo '<input type="checkbox" name="add_players[]" value="'.$row[$id].'" /></td>';echo '<td align="left">'.$row[$id].'</td>';echo '<td align="left">'.$row[$name].'</td>';echo '<td align="left">'.$row[$surname].'</td>';echo '<td align="center">'.$row[$ability].'</td>';echo '<td align="center">'.$row[$fitness].'</td>';echo '<td align="center">'.$row[$skill].'</td>';echo '<td align="center">'.$row[$position].'</td>';echo '<td align="left">'.$row[$prefmove].'</td>';}}?></form></table></td></tr></table></body></html>[/code] Link to comment https://forums.phpfreaks.com/topic/33964-inserting-multiple-values-into-table-via-check-boxes/#findComment-159600 Share on other sites More sharing options...
Barand Posted January 13, 2007 Share Posted January 13, 2007 Can you remember what you changed between it working and not working? Link to comment https://forums.phpfreaks.com/topic/33964-inserting-multiple-values-into-table-via-check-boxes/#findComment-159609 Share on other sites More sharing options...
joebudden Posted January 13, 2007 Author Share Posted January 13, 2007 Ignore me im dumb, it is working.Cheers barandIs there an easy way to prevent the user from inserting more than 5 players??? Link to comment https://forums.phpfreaks.com/topic/33964-inserting-multiple-values-into-table-via-check-boxes/#findComment-159612 Share on other sites More sharing options...
Barand Posted January 13, 2007 Share Posted January 13, 2007 You could set up a $count variable and increment it each time you insert then "break" when you get to 5or get the top 5 array elements and just use those[code]<?phpif(isset($_POST["add_players"])){ $addplayers = array_slice ($_POST["add_players"], 0, 5); // get first 5 foreach ($addplayers as $value) { //if ($value !== "Play Match") //{ $addsql = "insert into team (id , team) values ($value, 'user')"; mysql_query($addsql); //} }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/33964-inserting-multiple-values-into-table-via-check-boxes/#findComment-159617 Share on other sites More sharing options...
joebudden Posted January 13, 2007 Author Share Posted January 13, 2007 hmmm well ideally I want an error message to inform the user that they have selected more than 5 players and then reload the page.[code]<?php$players = $_POST["add_players"];$test = count($players);echo $test;?> [/code]I can now use this in an if statement cant i? Link to comment https://forums.phpfreaks.com/topic/33964-inserting-multiple-values-into-table-via-check-boxes/#findComment-159620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.