How to Store Check box in MYsql?
Am Newbie.I have this coding...How to store into Mysq开发者_如何学Cl and How to Retrieve from the Database?
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
am doubt with if i select both 2 box means what will happen? Please Explain...Thanks in Advance..
If you have no problem with comma separated values
my answer will help you
simple use array name to your field
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
then implode it with comma
implode(","$_POST['like']);
if two box is checked then
result will be yes,no
make your column in varchar Then do usual stuff to store
yourscript.php:
<?php
include("mysql_connect.php");
if ($_POST['like'])
{
if ($_POST['like'] != "yes" and $_POST['like'] != "no") die("Hacker");
mysql_query("INSERT INTO likes ('id', 'like') VALUES ('', '".$_POST['like']."')");
}
?>
<form method="post" action="yourscript.php">
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
</form>
retreive.php:
<?php
include("mysql_connect.php");
$likes = mysql_query("SELECT * FROM likes");
while($like = mysql_Fetch_assoc($likes))
{
echo "ID ".$like['id']." => ".$like['like'];
}
If you select both fields, it will save the latest select. You want radio (probably)
You probably don't want to be doing this in the first place. What you really want to be using is radio buttons for yes/no values. Otherwise, you're going to run into problems.
you can use like this
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
to store in db use serialize();
function.
unserialize()
return the same array with keys.
精彩评论