Link to home
Start Free TrialLog in
Avatar of CalmSoul
CalmSoulFlag for United States of America

asked on

insert checkbox value in database

hello,
here is my form code

      <tr>
            <td><b><font face="Verdana" size="2" color="#0000FF">Completed</font></b></td>
            <td><font face="Verdana"><input type="checkbox" name="C1" value="0"></font></td>
      </tr>

when I insert value in database ... checkbox value is not going

here is my insert.php code

<?php
//connection

  $sql="INSERT INTO requests (Request, Type, Completed)
VALUES
('$_POST[S1]','$_POST[D1]','$_POST[C1]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>
Avatar of mukhtar2t
mukhtar2t

the problem i think in ' value = "0" '
if you used any value other than 0 it will insert
or you can check it before inserting when it is empty you can insert 0
Avatar of CalmSoul

ASKER

so what is the solution?
<?php
//connection

if(!isset($_POST[C1]))
  $_POST[C1] = 0;
  $sql="INSERT INTO requests (Request, Type, Completed)
VALUES
('$_POST[S1]','$_POST[D1]','$_POST[C1]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>
Avatar of b0lsc0tt
I don't know that a zero would be a problem.  What type of field is Completed in your database?  You are checking the checkbox correct?

Part of me thinks you aren't and assuming the form sends the value anyways.  Maybe that assumption is wrong but I thought it was worth checking.  The form will only send the name and its value if the box is checked.

In your PHP script echo the value of $_POST["C1"] to see if the script does get the 0.  What is the result?

Let me know if you have a question or need more information.

bol
bol,

If the box is checked or unchecked it send out the "0" value in the database

complete is int field.... in database

In not all cases I am checking the checkbox... I might check or I might not check...

if checkbox is not checked I want "0" in database
if checkbox is checked I want "1" in database...
     <tr>
            <td><b><font face="Verdana" size="2" color="#0000FF">Completed</font></b></td>
            <td><font face="Verdana"><input type="checkbox" name="C1" value="1"></font></td>
      </tr>

<?php
//connection
if(!isset($_POST[C1]))
  $_POST[C1] = 0;

  $sql="INSERT INTO requests (Request, Type, Completed)
VALUES
('$_POST[S1]','$_POST[D1]','$_POST[C1]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>
That is your solution
if you check the box it will send the 1 and it will inserted in the db
if you didn't check the box it will send nothing and the if condition will fill the variable with 0
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@mukhtar2t - You have 2 major errors in the proposed code.  Besides missing brackets for the If statement you can't use $_POST[] like variables to be set or changed.

bol
I'm glad I could help you.  Thank you for the grade, the points and the fun question.

bol
A correction to my comment to Mukhtar2t.  Mukhtar2t, I'm sorry, PHP does let you change $_POST[].  For example, $_POST["C1"] = "changed"; would make that "changed" instead of the value sent from the form.

bol