Jump to content

inserting multiple values into table via check boxes


joebudden

Recommended Posts

Hi, am writing a script to allow users to select check boxes

  then insert the data into a new table

Here is my code :

[u]players.php:[/u]
[code]
<?php
session_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>
<?php
echo $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>
<?php
if (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 value

And i have tried a few things but always get the same result

Any ideas ???
Name the checkboxes like this, note the "[]"
[code]<?php
echo '<input type="checkbox" name="add_players[]" value="'.$row[$id].'" /></td>';
?>[/code]
then
[code]
foreach ($_POST['add_players'] as $value)  {

        //  insert $value into table

}
?>[/code]
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]
<?php
session_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>
<?php
echo $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>
<?php
if (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]
You could set up a $count variable and increment it each time you insert then "break" when you get to 5

or get the top 5 array elements and just use those

[code]<?php
if(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]
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?

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.