hi, i need some help on inserting checkbox values into the mysql database.

what sort of data type should i use for mysql to store these?

for example

i need to insert a few hobbies as checkbox

1.basketball
2.football
3.hockey

i need to be able to perform a search and retrieve these checkbox values.

You can do something like this
1.the form

<input type=checkbox  name=hobbie[] value="">

then when Inserting to myslq use Array))

<?php 
	session_start();
	error_reporting (E_ALL ^ E_NOTICE);
	include ('dbc.php'); 

if ($_POST['Submit'] == 'Register')


{
	//validation
	/* 
  	//check if username exist
	$rs_duplicates = mysql_query("select id from users where user_email='$_POST[email]'");
	$duplicates = mysql_num_rows($rs_duplicates);
	
	if ($duplicates > 0)
	
	{	
	//die ("ERROR: User account already exists.");
	header("Location: register.php?msg=ERROR: User account already exists..");
	exit();
	*/
}
	
		
	/*
	$md5pass = md5($_POST['pass2']);
	$activ_code = rand(1000,9999);
	$server = $_SERVER['HTTP_HOST'];
	$host = ereg_replace('www.','',$server);
	*/


	
	mysql_query("INSERT INTO check
	      		(`team`)
				  VALUES
				  ('$_POST[sports]')") or die(mysql_error());
	


?> 

<link href="styles.css" rel="stylesheet" type="text/css">

<?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>

<p>&nbsp;</p>

<table width="65%" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    	<td bgcolor="d5e8f9" class="mnuheader"><strong><font size="5">hobbies</font></strong></td>
  </tr>
  
  

  <tr> 
    	<td bgcolor="e5ecf9" class="forumposts"><form name="form1" method="post" action="register.php" style="padding:5px;">
        
    	<p><br>
    	<p>
    			<input type="checkbox" name="sports[]" value="football" />&nbsp;football<br />
    	    	<input type="checkbox" name="sports[]" value="hockey" />&nbsp;hockey<br />
    	    	<input type="checkbox" name="sports[]" value="baseball" />&nbsp;baseball</p> <p>
    	    
    	</p>
    				
    
      </form>
      
      </td>
  </tr>
</table>
<div align="left"></div>
</body>
</html>

i get this error

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 'check (`team`) VALUES ('')' at line 1

try adding this

$counter=0;
$sport=$_POST['sports'];
while($counter<=count($sport))
{
$result=mysql_query("INSERT INTO check
(team)
VALUES
('$sport')");
if($result){
echo" ($counter+1) added successfuly";
}

}



}

Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\em\login\register2.php on line 27

<?php 
	session_start();
	error_reporting (E_ALL ^ E_NOTICE);
	include ('dbc.php'); 

if ($_POST['Submit'] == 'Register')


{
	//validation
	/* 
  	//check if username exist
	$rs_duplicates = mysql_query("select id from users where user_email='$_POST[email]'");
	$duplicates = mysql_num_rows($rs_duplicates);
	
	if ($duplicates > 0)
	
	{	
	//die ("ERROR: User account already exists.");
	header("Location: register.php?msg=ERROR: User account already exists..");
	exit();
	*/
}
	
		
$counter=0;$sport=$_POST['sports'];
	while($counter<=count($sport)){$result=mysql_query("INSERT INTO check(team)VALUES('$sport')");
	
	if($result){echo" ($counter+1) added successfuly";} 
		}   
			
	
	$counter=0;
	$sport=$_POST['sports'];
		
	while($counter<=count($sport))
	{
		$result=mysql_query("INSERT INTO check
		(team)
			VALUES
			('$sport')");
	
			if($result){
			echo" ($counter+1) added successfuly";
	}

	}




?> 

<link href="styles.css" rel="stylesheet" type="text/css">

<?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>

<p>&nbsp;</p>

<table width="65%" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    	<td bgcolor="d5e8f9" class="mnuheader"><strong><font size="5">hobbies</font></strong></td>
  </tr>
  
  

  <tr> 
    	<td bgcolor="e5ecf9" class="forumposts"><form name="form1" method="post" action="register2.php" style="padding:5px;">
        
    	<p><br>
    	<p>
    			<input type="checkbox" name="sports[]" value="football" />&nbsp;football<br />
    	    	<input type="checkbox" name="sports[]" value="hockey" />&nbsp;hockey<br />
    	    	<input type="checkbox" name="sports[]" value="baseball" />&nbsp;baseball</p> <p>
    	    
    	</p>
    				
    
      </form>
      
      </td>
  </tr>
  
  <input type="submit" name="Submit" value="Register">
</table>
<div align="left"></div>
</body>
</html>

takes a long time to load with that error on top. i'm quite new to this...hope you can guide me through

my table contains two fields
1. "id" auto increment, not null
2. "team" varchar

dbc.php

<?php
$dbname = 'em';
$link = mysql_connect("localhost","root","") or die("Couldn't make connection.");
$db = mysql_select_db($dbname, $link) or die("Couldn't select database");
?>


write.php

<?

include ('dbc.php');

if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>

<form method="post" action="write.php">

<input type="checkbox" name="media[]" value="Poster" />bball<br />
<input type="checkbox" name="media[]" value="Website" />swim<br />
<input type="checkbox" name="media[]" value="Magazine" />run<br />
<input type="submit" value="submit" name="submit">
</form>

<?
} 

else {
include ('dbc.php');


$media_array = $_POST['media'];
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$media = substr($source, 0, -2);

$query = "INSERT INTO `em`.`check`(`id`, `team`)
VALUES('NULL','$media')";
$result = mysql_query($query);
}
?>
</body>
</html>

i modified my code, the form was posted without errors but the values are not recorded in mysql.

someone help pls

<?php
session_start();
error_reporting (E_ALL ^ E_NOTICE);
include ('dbc.php');



if ($_POST['Submit'] == 'Register')


{
//validation
/*
//check if username exist
$rs_duplicates = mysql_query("select id from users where user_email='$_POST[email]'");
$duplicates = mysql_num_rows($rs_duplicates);

if ($duplicates > 0)

{
//die ("ERROR: User account already exists.");
header("Location: register.php?msg=ERROR: User account already exists..");
exit();
*/

mysql_select_db("lastr");//check the last row inserted
if(isset($_POST['sports']))
{

foreach($_POST['sports'] as $team){


$insert="INSERT INTO check(team) VALUES ('$team')";
mysql_query($insert) or die(mysql_error());


}
}else{

echo"Nothing posted";
}
}
?>

<link href="styles.css" rel="stylesheet" type="text/css">

<?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>

<p>&nbsp;</p>

<table width="65%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="d5e8f9" class="mnuheader"><strong><font size="5">hobbies</font></strong></td>
</tr>
<tr>
<td bgcolor="e5ecf9" class="forumposts">
<form name="form1" method="post" action="<?php echo$_SERVER['PHP_SELF'] ?>" style="padding:5px;">

<p><br>
<p>
<input type="checkbox" name="sports[]" value="football" />&nbsp;football<br />
<input type="checkbox" name="sports[]" value="hockey" />&nbsp;hockey<br />
<input type="checkbox" name="sports[]" value="baseball" />&nbsp;baseball</p> <p>

</p>

</td>
</tr>

<input type="submit" name="Submit" value="Register">
</table>
</form>
<div align="left"></div>
</body>
</html>

i think, it's very close....

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 'check(team) VALUES ('football')' at line 1


my table contains two fields
1. "id" auto increment, not null
2. "team" varchar

is there something wrong with this setup ?

this is mysql data

Field Type Collation Attributes Null Default Extra
id int(11) none none No None auto_increment
team varchar(100) latin1_swedish_ci none No None none

how about this ??

$insert="INSERT INTO check SET team='$team'";

mysql_query($insert) or die(mysql_error());
commented: solved thanks a lot +1

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 'check SET team='football'' at line 1


same thing... :(

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 'check SET team='football'' at line 1


even when i check all the 3 box, the value of team is equals to football....shouldn't it be all 3 as a string?

The Code i wrote i guess everything is OK!
i hope somebody else can Help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.