Save Checkbox Value (1 or 0) With mySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    Save Checkbox Value (1 or 0) With mySQL

    I have a form with several checkboxes that I would like to save a bit value in the corresponding column in mySQL. As far as the html is concerned, I have each checkboxs' value set to '1'. I have created variables to capture the POST value for each html field, and then I use that variable in a SQL insert statement.

    Code:
    if(isset($_POST['action']) && $_POST['action'] == 'submitform')
    {
    	$personalizedname = $_POST['personalizedname'];
    	$personalizedaddress = $_POST['personalizedaddress'];
    	$personalizedphone= $_POST['personalizedphone'];
    	$mountain = $_POST['mountain'];
    	$surfboards = $_POST['surfboards'];
    	$bird = $_POST['bird'];
    	$sailboats = $_POST['sailboats'];
    	$beach = $_POST['beach'];
    	$name = $_POST['Name'];
    	$company = $_POST['Company'];
    	$address1 = $_POST['address1'];
    	$address2 = $_POST['address2'];
    	$citystatezip = $_POST['citystatezip'];
    	$phone = $_POST['phone'];
    	$email = $_POST['email'];
    	$comments = $_POST['comments'];
    	//$type = $_POST['type'];
    	$mailingbrochureoffer = $_POST['mailingbrochureoffer'];
    	$emailoffer = $_POST['emailoffer'];
    	$tradeshowoffer = $_POST['tradeshowoffer'];
    	$otheroffer = $_POST['otheroffer'];
            $othertext = $_POST['othertext'];
    	$couponcode = $_POST['couponcode'];
    }
    Code:
    $insert_query = sprintf("INSERT INTO BagTags (personalizedname, personalizedaddress, personalizedphone, mountain, surfboards, bird, sailboats, beach, name, company, address1, address2, citystatezip, phone, email, asippaiupicid, comments, mailingbrochureoffer, emailoffer, tradeshowoffer, otheroffer, othertext, couponcode) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'CompanyA', %s, %s, %s, %s, %s, %s, %s)",
    	sanitize($personalizedname, "text"),
    	sanitize($personalizedaddress, "text"),
    	sanitize($personalizedphone, "int"),
    	$mountain,
    	$surfboards,
    	$bird,
    	$sailboats,
    	$beach,
    	sanitize($name, "text"),
    	sanitize($company, "text"),
    	sanitize($address1, "text"),
    	sanitize($address2, "text"),
    	sanitize($citystatezip, "text"),
    	sanitize($phone, "int"),
    	sanitize($email, "text"),
    	sanitize($comments, "text"),
    	//sanitize($type, "text"),
    	$mailingbrochureoffer,
    	$emailoffer,
    	$tradeshowoffer,
    	$otheroffer,
    	sanitize($othertext, "text"),
    	$couponcode);
    On the database side, the columns where the checkboxes will be saved are currently varchar(32) with a default value of '0'. I plan to change them back to a bit value once I have things working correctly. Currently, I receive a SQL error if any of the checkboxes are unchecked. If all of the checkboxes are checked, the value in the checkbox columns does not equal '1' as I would have expected.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Unchecked check boxes are not set as a POST[] element.
    So you need something like
    Code:
    if(isset($_POST['checkbox']) { 
     $var = 1; 
    } else { 
     $var = 0;
    You are blindly placing a lot of form data into a DB with no validation,
    not only is this bad practice, but it leads to problems such as yours

    Comment

    • mcfly1204
      New Member
      • Jul 2007
      • 233

      #3
      I realize now that there is no value returned for unchecked checkboxes. Thanks for the assumption in the last tip chief.

      Comment

      • s4mm77
        New Member
        • Oct 2012
        • 1

        #4
        I might be missing some vital point here but...

        Code:
        $var = isset($_POST['checkbox']);
        I mean, you wouldn't even need the whole $var, just surround it with isset()? It's what i've been doing ._.

        Comment

        • viirre
          New Member
          • Nov 2012
          • 1

          #5
          IF you have an automated insert/update procedure, it's pretty easy to just output a hidden input with value = 0 right before the checkbox:
          <input type="hidden" name="cb" value="0" />
          <input type="checkbox" name="cb" value="1" />

          Now you'll allways either get 0 or 1 from the POST

          Comment

          Working...