Using checkboxes to update multiple database entries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mountain.dog@gmail.com

    #1

    Using checkboxes to update multiple database entries

    I have a query that shows a list of options that a user can toggle on
    or off using a checkbox.

    query...
    form...
    while($row = mysql_fetch_arr ay($result))...
    <input name="menu_show _attribute[]" type="checkbox"
    class="checkbox "');
    if ($row['menu_show_attr ibute'] == 1) {
    echo (' value="'.$row[menu_id].'" checked />
    } else {
    echo (' value="'.$row[menu_id].'" />');
    }

    The list is generated from boolean values in a DB. This populates my
    form with checkboxes, some are checked and others are not. I'm using
    an if statement to sort it (checked). This works fine.

    The problem is when the user un-checks a checkbox, the value does not
    get passed and the DB does not update - set/change it to 0. The only
    values that get passed are the checkboxes that are checked. Below is
    the query / code after the submit button has been pressed. Are radio
    buttons the answer? Any help / suggestions would be very much
    appreciated.

    //process form
    for ($i = 0; $i < count($menu_sho w_attribute); $i++) {

    if (isset($menu_id ) == 'checked') {
    $menu_show_attr ibute = 1;
    } elseif ($menu_show_att ribute != 'checked') {
    $menu_show_attr ibute = 0;
    }

    $query = 'UPDATE menu SET menu_show_attri bute = "'.
    $menu_show_attr ibute.'" WHERE menu_id = "'.
    $menu_show_attr ibute[$i].'"';
    echo("<br>");
    echo $query;
    $result = mysql_query($qu ery, $db) or die(mysql_error ());
    }

  • Geoff Berrow

    #2
    Re: Using checkboxes to update multiple database entries

    Message-ID: <1175177552.525 042.244720@b75g 2000hsg.googleg roups.comfrom
    mountain.dog@gm ail.com contained the following:
    >The problem is when the user un-checks a checkbox, the value does not
    >get passed and the DB does not update - set/change it to 0. The only
    >values that get passed are the checkboxes that are checked. Below is
    >the query / code after the submit button has been pressed. Are radio
    >buttons the answer? Any help / suggestions would be very much
    >appreciated.
    Checkboxes only return a value if checked. The solution is to make your
    update query update all fields with the default value set to unchecked.
    The unchecked fields will automatically be unchecked because the POSTed
    value will not exist.

    Of course you will have to explicitly name your check boxes something
    like <input name='menu_show _attribute[".$row['menu_id']."]'
    type='checkbox'
    class='checkbox ')

    And of course you won't be able to loop through the array
    $_POST['menu_show_attr ibute'] because the unchecked values will not
    show. However you could include a hidden field
    <input name='hidden_me nu_id[".$row['menu_id']."]' type='hidden'>

    Then update your db by looping through $_POST['hidden_menu_id ']

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • mountain.dog@gmail.com

      #3
      Re: Using checkboxes to update multiple database entries

      Thanks. I had a feeling checkboxes were not going to work. Oh well.
      Do think radio buttons would work?

      On Mar 29, 9:43 am, Geoff Berrow <blthe...@ckdog .co.ukwrote:
      Message-ID: <1175177552.525 042.244720@b75g 2000hsg.googleg roups.comfrom
      mountain....@gm ail.com contained the following:
      >
      The problem is when the user un-checks a checkbox, the value does not
      get passed and the DB does not update - set/change it to 0. The only
      values that get passed are the checkboxes that are checked. Below is
      the query / code after the submit button has been pressed. Are radio
      buttons the answer? Any help / suggestions would be very much
      appreciated.
      >
      Checkboxes only return a value if checked. The solution is to make your
      update query update all fields with the default value set to unchecked.
      The unchecked fields will automatically be unchecked because the POSTed
      value will not exist.
      >
      Of course you will have to explicitly name your check boxes something
      like <input name='menu_show _attribute[".$row['menu_id']."]'
      type='checkbox'
      class='checkbox ')
      >
      And of course you won't be able to loop through the array
      $_POST['menu_show_attr ibute'] because the unchecked values will not
      show. However you could include a hidden field
      <input name='hidden_me nu_id[".$row['menu_id']."]' type='hidden'>
      >
      Then update your db by looping through $_POST['hidden_menu_id ']
      >
      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDshttp://www.ckdog.co.uk/rfdmaker/

      Comment

      • Geoff Berrow

        #4
        Re: Using checkboxes to update multiple database entries

        Message-ID: <1175180377.334 506.304110@l77g 2000hsb.googleg roups.comfrom
        mountain.dog@gm ail.com contained the following:
        >Thanks. I had a feeling checkboxes were not going to work.
        Where did I say that? Of course checkboxes will work.
        >Do think radio buttons would work?
        No.
        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        Working...