how do i insert a NULL from an option dropdown?

Discussion in 'PHP' started by pedrotuga, Jun 19, 2006.

  1. #1
    ok... here is my problem...

    how do i insert a null on the database?

    this

    <oprtion value=""></option>

    doesnt insert a null
     
    pedrotuga, Jun 19, 2006 IP
  2. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You will have to see if the value is equal to "" then substitue that for that code that will produce a NULL within PHP. Sorry I dont have more info as PHP isn't my language.
     
    ServerUnion, Jun 19, 2006 IP
  3. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #3
    value="" does not mean that its null. rather it means empty string. you know the difference between empty string and null value


    yes nor php is my language.

    but I can tell you to insert null value you have check programettically certain condition and insert NULL then
     
    kashem, Jun 19, 2006 IP
  4. pedrotuga

    pedrotuga Peon

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks for the help
     
    pedrotuga, Jun 19, 2006 IP
  5. prch

    prch Peon

    Messages:
    11
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I don't think it's possible to insert the field as NULL with PHP. You can only insert it as a "string" instead of identified as "unknown data" (NULL).

    Set that field option within DB as "ALLOW NULL". Any empty value or "" inserted will be treated or updated as NULL. :cool:
     
    prch, Jun 23, 2006 IP
  6. TrippAllen

    TrippAllen Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    When you're checking for the value of the drop down, you might want to make the value of your empty option "null". This gives PHP something to check against for how to run the sql query.
    I'm assuming that what you're wanting to do is start with an initially selected option that gives 'em instructions or ensures that they don't overlook the dropdown by leaving it on the first value.

    Let's assume that you're doing this for some sort of shopping cart and the dropdown deals with the color of a product. Options might be blue, red, green. So, you'd have a dropdown with name/value pairs of "Select Color"/"null" , "Blue"/"b" , "Red"/"r" , "Green"/"g"

    So, you'd do the following:

    if($dropdown == "null")
    {
    $sql_insert="INSERT INTO product (product_color) values (null)";
    //Note that the value null is not in quotes so it isn't interpreted as a string with the characters n-u-l-l, but is instead a predefined value.
    }
    else
    {
    $sql_insert="INSERT INTO product (product_color) values ('$dropdown')";
    }

    In theory, this should work. It might be worth a try though, as it's just one if statement that isn't going to drastically change things.
     
    TrippAllen, Jun 23, 2006 IP
  7. pedrotuga

    pedrotuga Peon

    Messages:
    162
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks trippallen, thats what i suspected... i have to write an extra query for nulls :(
    pretty anoying

    everything works ok with "" on suposly null fields... but i know that that is not the right way to do it...
    gota fix my code all over, boring... i will leave this minor bug to later.
    Anyway... now i already know that i have to use a condition. thx
     
    pedrotuga, Jun 23, 2006 IP