Solved

insert checkbox value in database

Posted on 2007-03-29
12
216 Views
Last Modified: 2008-02-01
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)
?>
0
Comment
Question by:CalmSoul
  • 5
  • 5
  • 2
12 Comments
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18818039
the problem i think in ' value = "0" '
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18818046
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
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18818049
so what is the solution?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18818073
<?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)
?>
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18818092
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
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18818197
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...
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18818237
     <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)
?>
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18818251
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
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 500 total points
ID: 18818280
>> If the box is checked or unchecked it send out the "0" value in the database <<
What do you mean.  If the form field isn't checked then that checkbox name and the value 0 aren't in $_POST.

>> if checkbox is not checked I want "0" in database
if checkbox is checked I want "1" in database...  <<

If that is what you want to have happend then you should use ...

if (isset($_POST["C1"])) {
    $c1 = '1';
} else {
    $c1 = '0';
}

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

If the database field is a type of number then you probably don't need the single quotes around that value.

Let me know how that works or if you have a question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18818314
@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
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18818320
I'm glad I could help you.  Thank you for the grade, the points and the fun question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18818408
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
000webhost.com default error log 1 40
PHP Installer 5 37
Ahax pagination 9 32
PHP website on Linux - server DNS address could not be found. 18 45
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question