PHP/MySQL Help

Discussion in 'PHP' started by TubeTweaks, Aug 9, 2010.

  1. #1
    So I wasnt sure which section to post this in as it deals with PHP and MySQL, However.. here's my issue.

    I have a registration form that was pre generated through a form generator. I wanted to add a check box to people can choose weather or not they want to subscribe to the newsletter. I added the checkbox to the form, and modified the code so it would insert a value into the SQL database.

    The problem is, weather it's checked or not it simply puts the value of '0' into the column. My goal here is to have a 0 if unchecked, and a 1 if checked. Any suggestions?
     
    TubeTweaks, Aug 9, 2010 IP
  2. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's because a checkbox is "on" on the callback from _POST/_GET.

    You'll have to do something along the lines of:
    $myCheckValue = $_POST["myCheckBox"];

    if ($myCheckValue == "on") { $myCheckFieldValue = 1; } else { $myCheckFieldValue = 0; }

    Then in your SQL you will be writing the $myCheckFieldValue variable assuming the field is of bool (tinyint) type.
     
    AntelopeSalad, Aug 9, 2010 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    if checkbox is checked, value would be "on"
     
    baris22, Aug 9, 2010 IP
  4. TubeTweaks

    TubeTweaks Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks antelope!! Will report back, hopefully everything goes well!
     
    TubeTweaks, Aug 9, 2010 IP
  5. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #5
    You set the value of the checkbox when you make it, add value="0" for the no option and value="1" for the yes option. Then insert that into your database like,

    PHP
    
    $checkbox = $_POST['checkbox'];
    
    PHP:
    MySQL
    
    INSERT INTO table (checkbox) VALUES ('$checkbox')
    
    Code (markup):
     
    HuggyEssex, Aug 9, 2010 IP