Checkbox forms, php & mysql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • elyob

    #1

    Checkbox forms, php & mysql

    Okay, I'm about to add some checkboxes to a form, and then store the data in
    a column on a MySQL database. Just wanted to know best practice, at the
    moment I'm writing and thinking .. God that's ungly code ...

    <input type="checkbox" name="delta" <?php if ($delta==1) { echo
    "checked"; }?>> Delta<br>
    <input type="checkbox" name="mastercar d" <?php if ($mastercard==1 ) {
    echo "checked"; }?>> Mastercard<br>
    <input type="checkbox" name="solo" <?php if ($solo==1) { echo
    "checked"; }?>> Solo<br>
    <input type="checkbox" name="switch" <?php if ($switch==1) { echo
    "checked"; }?>> Switch<br>
    <input type="checkbox" name="visa" <?php if ($visa==1) { echo
    "checked"; }?>> Visa<br>

    MySQL database fields ...
    delta binary(1) No 0
    mastercard binary(1) No 0
    solo binary(1) No 0
    switch binary(1) No 0
    visa binary(1) No 0

    When I output the fields, I get "on" or empty, and am wondering whether I am
    making a good decision to do the code this way. Now I need to figure out how
    to update the database based on the forms output.

    Thanks


  • Rik

    #2
    Re: Checkbox forms, php &amp; mysql

    elyob wrote:[color=blue]
    > Okay, I'm about to add some checkboxes to a form, and then store the
    > data in a column on a MySQL database. Just wanted to know best
    > practice, at the moment I'm writing and thinking .. God that's ungly
    > code ...
    >
    > <input type="checkbox" name="delta" <?php if ($delta==1) { echo
    > "checked"; }?>> Delta<br>
    > <input type="checkbox" name="mastercar d" <?php if
    > ($mastercard==1 ) { echo "checked"; }?>> Mastercard<br>
    > <input type="checkbox" name="solo" <?php if ($solo==1) { echo
    > "checked"; }?>> Solo<br>
    > <input type="checkbox" name="switch" <?php if ($switch==1) { echo
    > "checked"; }?>> Switch<br>
    > <input type="checkbox" name="visa" <?php if ($visa==1) { echo
    > "checked"; }?>> Visa<br>
    >
    > MySQL database fields ...
    > delta binary(1) No 0
    > mastercard binary(1) No 0
    > solo binary(1) No 0
    > switch binary(1) No 0
    > visa binary(1) No 0
    >
    > When I output the fields, I get "on" or empty, and am wondering
    > whether I am making a good decision to do the code this way. Now I
    > need to figure out how to update the database based on the forms
    > output.[/color]

    On the right track IMO.

    You could query the database like this (Warning:I'm an xhtml-fetishist):

    SELECT `id`, `some_field`, IF(`delta` = 1, ' checked="checke d"','') AS
    'delta', etc.....

    And, assuming you have a $row array resulting form the query:

    echo "<input type=\"checkbox \" name="delta"{$r ow['delta']} />";

    On submitting the update:
    $delta = (isset($_POST['delta']))?'1':'0';

    "UPDATE table SET delta='$delta', ect...."

    Grtz,
    --
    Rik Wasmus


    Comment

    • Jerry Stuckle

      #3
      Re: Checkbox forms, php &amp; mysql

      elyob wrote:[color=blue]
      > Okay, I'm about to add some checkboxes to a form, and then store the data in
      > a column on a MySQL database. Just wanted to know best practice, at the
      > moment I'm writing and thinking .. God that's ungly code ...
      >
      > <input type="checkbox" name="delta" <?php if ($delta==1) { echo
      > "checked"; }?>> Delta<br>
      > <input type="checkbox" name="mastercar d" <?php if ($mastercard==1 ) {
      > echo "checked"; }?>> Mastercard<br>
      > <input type="checkbox" name="solo" <?php if ($solo==1) { echo
      > "checked"; }?>> Solo<br>
      > <input type="checkbox" name="switch" <?php if ($switch==1) { echo
      > "checked"; }?>> Switch<br>
      > <input type="checkbox" name="visa" <?php if ($visa==1) { echo
      > "checked"; }?>> Visa<br>
      >
      > MySQL database fields ...
      > delta binary(1) No 0
      > mastercard binary(1) No 0
      > solo binary(1) No 0
      > switch binary(1) No 0
      > visa binary(1) No 0
      >
      > When I output the fields, I get "on" or empty, and am wondering whether I am
      > making a good decision to do the code this way. Now I need to figure out how
      > to update the database based on the forms output.
      >
      > Thanks
      >
      >[/color]

      Are these mutually exclusive? Or can they have mastercard and visa checked at
      the same time?

      If they're mutually exclusive, a better way is to use a single field to hold a
      value (perhaps an ENUM). Otherwise - what i f due to some problem you have both
      mastercard and visa on in the field?


      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Jason Herald

        #4
        Re: Checkbox forms, php &amp; mysql

        Another option is to loose the binary and use a text field and just
        store the on or blank. It really just depends on what you are going
        for.

        Using int you would do it such:

        $d = null;
        if ($_POST['check_box_name ']=="on") {
        $d = 1;
        } else {
        $d = 0;
        }

        Then just update your database accordingly.

        Comment

        Working...