Checkbox PHP SQL

Discussion in 'PHP' started by johneva, Feb 12, 2010.

  1. #1
    Hi

    I already have a control panel together that adds,edits and removes vehicles on a my SQL database, what I need to do though is add a check box and have the data added to the database too.

    I can add the checkbox to the form no probs of course, but how do I go about adding the extra feild needed for a posative negative answer from the check box?

    The reason I want to do this is some of the vehicles will be special offers and will need to also be called for on a different display page aslong as they have been ticked off as a special offer on the add form from the control panel.

    Cheers for any help on this matter, been looking round but only finding stuff for multiple check box data to sql.
     
    johneva, Feb 12, 2010 IP
  2. thorie

    thorie Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A couple ways to do it comes to mind:
    1) add a column for each checkbox and use BOOL to store 0 (unchecked) or 1 (checked). it's easier to update, but takes up more space in the database and not very flexible when it comes to adding new checkboxes.
    2) create another table with two columns - the ID of the form data, and the checkbox name. when you update this, you'll have to delete the rows that are unchecked, each time.

    Makes sense?
     
    thorie, Feb 12, 2010 IP
    johneva likes this.
  3. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #3
    I have added a field in the database using Bool to store and added the check box on the form now but it dont seem to change the value from 0 to 1 when I check the box when adding a vehicle.

    New input on form:
    Special: <input type="checkbox" name="special" /><br />
    HTML:
    Is there something special I need to do as its a checkbox data going to bool style data on database?

    On the process form file I simpley made a variable from the post data, then used insert to insert the data.

    
    // added section for form details 
    $make=$_POST['make']; 
    $model=$_POST['model']; 
    $spec=$_POST['spec']; 
    $seven=$_POST['seven']; 
    $twelve=$_POST['twelve']; 
    $eighteen=$_POST['eighteen'];
    $special=$_POST['special'];
    
    
    //write data to database
    mysql_connect("localhost", "johnmev_user1", "Pass1") or die(mysql_error()); 
    mysql_select_db("cp") or die(mysql_error()); 
    mysql_query("INSERT INTO `data` (id, make, model, spec, seven, twelve, eighteen, special, database_filename)
    VALUES ( NULL, '$make', '$model', '$spec', '$seven', '$twelve', '$eighteen','$special', '$database_filename')");
    
    PHP:
     
    johneva, Feb 12, 2010 IP
  4. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #4
    I got it working now I just had to set the default value as 0 in the database and set the checkbox value to 1.
     
    johneva, Feb 12, 2010 IP
  5. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    And also, just for future reference, because I know this pops up in a coders lifetime. If you want it to be 'pre' selected when we open the page (For whichever one is chosen in the database) you can simply put something along the lines of.

    
    $checked = $var['from_db_1_or_0'];
    
    if ($checked) { // If checked =s 1
     $checked = 'checked';
    } else {
     $checked = null;
    }
    
    PHP:
    And then in your html code
    <input type='checkbox' name='checkbox1' value='myValue' <?php echo $checked; ?>>
    PHP:
     
    ProtegeSales, Feb 12, 2010 IP