SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Rio's Avatar
    Join Date
    Nov 2001
    Location
    United Kingdom
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox and MySQL's ENUM field

    Hi there,

    I have a checkboxs like the one below in my form page, to be used with MySQL fields set to ENUM('Y','N') NOT NULL DEFAULT 'N'.
    Code:
    <input name="$name" type="checkbox" value="Y" />
    When it's checked it my PHP script happily insert "Y" (please ignore the brackets) into MySQL but otherwise the field is left blank. Does anyone know why?

    Rio

  2. #2
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If a checkbox is not activated (checked) it does not pass a value (as if it would not exist), so only if it's checked you get a value (Y in your case).
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  3. #3
    SitePoint Zealot Rio's Avatar
    Join Date
    Nov 2001
    Location
    United Kingdom
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even when the default value is set 'N' ?

    That's being the case, do I have to tweak my PHP script to set the value of $name to 'N' ?

  4. #4
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The field should stay at N as long as no other value is submitted. Once it was Y it doesn't change to N again.

    For doing this, you need a little PHP scripting.
    Here's an excerpt from my script:
    PHP Code:
    /**
    * If variable is set, set it to the set_checked otherwise set it to the
    * unset_checked. Used to handle checkboxes when you are expecting them from
    * a form.
    */
    function check_checked(&$var$set_checked 1$unset_checked 0)
    {
    if (empty(
    $var))
    {
    $var $unset_checked;
    }
    else
    {
    $var $set_checked;
    }
    }


    function 
    updateNewsletter($frm)
    {
    check_checked($frm['n_deleted']);

    $sql db_query("UPDATE "DB_PREFIX ."newsletter
    SET n_title = '
    $frm[n_title]',
    n_description = '
    $frm[n_description]',
    n_deleted = '
    $frm[n_deleted]',
    n_modified = NOW()
    WHERE n_id = '
    $frm[n_id]'
    );

    Although i use 1 and 0 as value instead of Y and N

    EDIT:
    Forgot to mention that i don't use ENUM's but bitfield (TINYINT or INT) for 0 or 1. Might make things easier for you.
    Last edited by frezno; Sep 18, 2003 at 02:47.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •