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

Store checkbox value in database

HI,

I have a form that has more than one checkbox.
I need to store the checkbox value in mysql.
I am using php code.I am not sure of the code of storing the checkbox value to the database.
Your help will be much appreciated.
Thanks

Tryphy

tryphy
Junior Poster in Training
79 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Can you show us your code ?

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

Hopefully this will give you an idea that you can adapt into your code, if not then ask again

<?php
if(isset($_POST['box01']))
{
$box01 = 1;
}
else
{
$box01=0;
}
if(isset($_POST['box02']))
{
$box02=1;
}
else
{
$box02=0;
}

$insert=mysql_query("
INSERT INTO tablename('field01', 'field02')
VALUES ('" . $box01 . "', '" . $box02 . "'");
hooray
Junior Poster in Training
62 posts since Jan 2008
Reputation Points: 11
Solved Threads: 6
 

Hopefully this will give you an idea that you can adapt into your code, if not then ask again

<?php
if(isset($_POST['box01']))
{
$box01 = 1;
}
else
{
$box01=0;
}
if(isset($_POST['box02']))
{
$box02=1;
}
else
{
$box02=0;
}

$insert=mysql_query("
INSERT INTO tablename('field01', 'field02')
VALUES ('" . $box01 . "', '" . $box02 . "'");

Instead of $box01=1 and $box01=0, you can assign the value of the checkbox itself! $box01=$_POST['box01']; And this isn't useful if you have, say, 10 checkboxes. The best thing would be to have an array of checkboxes !

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

You make a good point, but would that store 'checked' or 'unchecked' in the database??

hooray
Junior Poster in Training
62 posts since Jan 2008
Reputation Points: 11
Solved Threads: 6
 
You make a good point, but would that store 'checked' or 'unchecked' in the database??

No. It will store the value of the checkbox. Eg.

<?php
print "<pre>";
print_r($_POST);
print "</pre>";
?>
<form method="POST" action="test.php">
<input type="checkbox" name="check[]" value="100">100 
<input type="checkbox" name="check[]" value="200">200 
<input type="checkbox" name="check[]" value="300">300 
<input type="submit" name="submit" value="submit">
</form>


Cheers,
Naveen

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

<?php
include("config.php");

$pid=$_POST['list'];
echo $pid;
$desc=$_POST['atxt'];

$sql="select * from `ph_category` where `id`='$pid'";
$q=mysql_query($sql);
$row=mysql_fetch_array($q);
$var=$row['category'];


$path="Uploads/";
$path=$path.basename($_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'],$path))
{
$filename=$path;
echo 'done';

} $var1=$_FILES['file']['name']; $img=$_FILES['file']['name'];; echo "$img"; include("checkbox.php"); if(isset($_POST['c1'])) { c1.checked="1"; } $sql="INSERT INTO `ph_cate` VALUES (' ','$pid','$var1','$var','$desc',' ',' $c1', ' $c2',' ') ";

$q=mysql_query($sql) or die(mysql_error());
echo "Data Submitted Successfully...";
?>

Geetha22
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Name list
<?php while($nt=mysql_fetch_array($result))
{
?>
<?php echo $nt[Name]?>

<?php
}
?>


Person info will be listed here.

vivekdizzal
Newbie Poster
4 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You