Saving Null Value

Discussion in 'PHP' started by MikeLiao, Oct 31, 2008.

  1. #1
    I know that saving data to mySQL is something like this:
    mysql_query("INSERT INTO mytable(field1, field2, field3, field4) VALUES ('$firstname', '$middlename', '$lastname', '$address')") or die(mysql_error());

    But field2 (middlename) and field4 (address) is optional. It may or may not have a value. In case it is blank, I should insert a NULL value.

    Every time it is blank my system always insert a space (not null)

    How can I check if a blank entry and then pass a null value? I know I can use the word "null" to pass a null value but I dont know how to implement this. Thanks

    By the way my fields allow null value.
     
    MikeLiao, Oct 31, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Are you using NULL without quotes?
    VALUES (null, '$middlename', '$lastname', '$address');
    PHP:
    Jay
     
    jayshah, Oct 31, 2008 IP
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    You can use if statement

    Say if the value entered is null than $middlename = "null";
     
    Bohra, Oct 31, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    No :)

    That would set it to the string "null" that has 4 characters.
    $middlename = null;
    PHP:
    That would be correct. NULL is case in-sensitive.
     
    jayshah, Oct 31, 2008 IP
  5. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #5
    my mistake it should be $middlename = null;

    Rep Added

    Edit

    Cant Give Rep asks to share with other ppl before giving you again
     
    Bohra, Oct 31, 2008 IP
  6. MikeLiao

    MikeLiao Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If I use this
    VALUES (null, '$middlename', '$lastname', '$address');

    it insert null. But what Im asking is how can I insert value depending on the user input, Meaning if the user did not enter any value, then I should insert null, but if the user inputted something then I should save it.

    If I use this:

    $middlename = null;
    VALUES ('$middlename', '$lastname', '$address');

    it inserts space
     
    MikeLiao, Oct 31, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    That because you are inserting 'null character'.

    Here's an example of what you want:
    
    if (empty($middlename)){
        $middlename = null;
    } else {
        $middlename = "'{$middlename}'";
    }
    // ...
    mysql_query("... VALUES ($middlename, '$lastname', '$address');");
    
    PHP:
    I don't really see the point, though. If the value is empty, it's going to be '' anyway :|
     
    jayshah, Oct 31, 2008 IP
  8. MikeLiao

    MikeLiao Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi jayshah,

    if I use this
    if (empty($middlename)){
    $middlename = null;
    ....

    VALUES ('$middlename', '$lastname', '$address');

    Im sure it inserts space instead of null
     
    MikeLiao, Oct 31, 2008 IP
  9. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #9
    That means the value of $middlename is already a space. Make sure it's totally empty. You can change the test:
    
    if (trim($middlename) == ''){
    PHP:
    Jay
     
    jayshah, Oct 31, 2008 IP
  10. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Is it submitted using a form? I assume it is:

    It may be a space or a newline character (if that input is a textarea).

    try var_dump()ing the eg: $_POST['middlename'] and see what it contains
     
    ads2help, Oct 31, 2008 IP
  11. MikeLiao

    MikeLiao Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Here's my actual code:

    if (trim($_POST['middle_name']) == "") {
    $my_midname = NULL;
    } else {
    $my_midname = trim($_POST['middle_name']);
    }


    ... VALUES ('$my_lastname', '$my_midname', '$my_firstname'.....)

    and it inserts space instead of null
     
    MikeLiao, Oct 31, 2008 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    Is your field NOT NULL by default? I mean when you ADD the field, like:

    ALTER TABLE `the_table` ADD `middle_name` NOT NULL ?

    If yeah, thats the problem i think.
     
    ads2help, Oct 31, 2008 IP
  13. MikeLiao

    MikeLiao Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hi ads2help,

    My original post says "By the way my fields allow null value."
     
    MikeLiao, Oct 31, 2008 IP