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!
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!
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!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356