954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

inserting checkbox values in mysql +PHP

select team:
:Argentina
:Spain
:Holland
:France

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

what's the line 10? I think you have left a bracket missing.

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 
<?php
if(isset($_POST['team']))
{
	foreach($_POST['team'] as $value){
		$insert=mysql_query("INSERT INTO team('team') VALUES ('$value')");
	}
}
?>
<html>
<body>
<form method="post" action="chk123.php">
<input type="checkbox" name="team[]" value="AG"> Argentina 
<input type="checkbox" name="team[]" value="GE"> Germany 
<input type="checkbox" name="team[]" value="BR"> Brazil 
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>


There! That will work :)

Cheers,
Naveen

Edit: The error was because, you had opened php tag but you opened it again.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Thank you :)

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

:) You are welcome!

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 



Argentina
Germany
Brazil

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

missing } after die function.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

It processes, but nothing is stored in the Mysql, table:(

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
$insert="INSERT INTO team('team')
VALUES ('$value')";
echo $insert;
mysql_query($insert);

Print your query. Execute the same in phpmyadmin or mysql. Check what's the error. Or you can give, mysql_query($insert) or die(mysql_error()); to know what is the error.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hi, i have tried this but doesn't return anything.

I'm Wondering whether my table design in mysql is incorrect.

I have only two collumns, one to store the ID and one to store the team.

Table Team
id Int(11) Not Null auto_increment
team varchar(30) Not Null


<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['team']))
{ foreach($_POST['team'] as $value){
$insert=mysql_query("INSERT INTO team('team')
VALUES ('$value')");
}
}

echo $insert;mysql_query($insert);

?>

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
if(isset($_POST['team'])) {    foreach($_POST['team'] as $value) {
    $insert="INSERT INTO team ('team') VALUES ('$value')";
echo $insert;
mysql_query($insert);
}
}

Try this. It will print out the query. Execute it in phpmyadmin or mysql. I don't think your query or the table structure is wrong. Maybe the values of $_POST['team'] isn't being passed. Well, execute these queries and find out why its not inserting !

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

I tried the query it prints "INSERT INTO team ('team') VALUES ('BR')" correctly however it still doesnt store the value into the database, I even selected mysql_select_db("lastr", $con);$sql= the database still not working.

thank you for all your responses by da way.

<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['team']))
{ foreach($_POST['team'] as $value) {
mysql_select_db("lastr", $con);$sql= $insert="INSERT INTO team ('team')
VALUES ('$value')";echo $insert;mysql_query($insert);
}
}

?>

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

umm.. You don't have a mysql_select_db in your script. If it isn't inserting, use die function with mysql_error with your query. For eg.

$query="select * from tablename";
mysql_query($query) or die(mysql_error());

So, if there's any problem with your query, it will output the error message, which you can use to debug your query.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

I have tried it with this function "mysql_query($query) or die(mysql_error());"

there is no error and nothing is still being inserted into the database :(.


<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['team']))
{ foreach($_POST['team'] as $value) {
mysql_select_db("lastr", $con);$sql= $insert="INSERT INTO team ('team')
VALUES ('$value')";echo $insert;mysql_query($insert);
}
}


$query="select * from team";mysql_query($query) or die(mysql_error());

?>

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Okay! Take out 'team' and put team. ie.,
$insert="INSERT INTO team (team)
VALUES ('$value')";

Tellme if it works.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Still the same, Nothing going into the database, maybe should insert into id aswell.


<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if(isset($_POST['team']))
{ foreach($_POST['team'] as $value) {
mysql_select_db("lastr", $con);$sql= $insert="INSERT INTO team. ('team')
VALUES ('$value')";echo $insert;mysql_query($insert);
}
}


$query="select * from team";mysql_query($query) or die(mysql_error());
?>

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Umm.. is Id an auto-increment field ? What did echo $insert print ? Could you show us the statement ? Did you execute that statement in phpmyadmin/mysql ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

INSERT INTO team ('team') VALUES ('GE')INSERT INTO team ('team') VALUES ('BR')

the id is set to auto_increment, when i run the query on phpadmin this is the error.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''team') VALUES ('GE')INSERT INTO team ('team') VALUES ('BR')' at line 1

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

:) That's what I was talking about.

<?php
$con = mysql_connect("localhost","root","talk21");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("lastr");

if(isset($_POST['team']))
{ 
     foreach($_POST['team'] as $value) {
          $insert="INSERT INTO team (team) VALUES ('$value')";
     mysql_query($insert);
    }
}
?>


This will definitely work. :)

P.S: Next time you post your code, please post it within[code] tags.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Great, many thanks for your help :). say if i wanted to do a dynamic table in mysql to insert the checkbox values from is there any threads you could point me to for example or help.

asadalim1
Light Poster
41 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You