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

Recommended Answers

All 7 Replies

Can you show us your code ?

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 . "'");

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 !

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

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 <br />
<input type="checkbox" name="check[]" value="200">200 <br />
<input type="checkbox" name="check[]" value="300">300 <br />
<input type="submit" name="submit" value="submit">
</form>

Cheers,
Naveen

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

$pid=$_POST;
echo $pid;
$desc=$_POST;

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


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

}
$var1=$_FILES;
$img=$_FILES;;
echo "<img src='$img'>";
include("checkbox.php");
if(isset($_POST))
{
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...";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
include('confi.php');
$sql = "select * from user";
$result = mysql_query($sql);
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?id="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>

<form><select name="users" onchange="showUser(this.value)">
      <option name="student" value=''>Name list</option>
  <?php while($nt=mysql_fetch_array($result))
  {
       ?>
    <option value="<?php echo $nt[id] ?>"><?php echo $nt[Name]?></option>    



<?php
  }
  ?>
  </select>
   </form>
   <br />
  <div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
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.