Updating multiple colums in mysql table with null values using PHP

Discussion in 'PHP' started by rvaibhav, Jan 23, 2011.

  1. #1
    Hi All,

    I am adding a feature of profile update in the profile page of a project.

    Now there might be a situation where user does not fill all the fields of his profile.

    Like through the profile page a user can update Name, About Me, Location, Country, Date Of Birth, Interests or His Website Link.

    The SQL query I am using is

    UPDATE userinfo SET name='$name',aboutme='$aboutme',location='$location',country='$country',interests='$interests',dob='$dob',website='$website' WHERE uid='".$_SESSION['user_id']."'

    Suppose,
    if the name is left blank in the form while updating I am updating $name=NULL
    if the country is left blank in the form while updating I am updating $country=NULL
    if the interestsis left blank in the form while updating I am updating $interests=NULL

    So now when I fire this query in the database using the PHP command mysql_query function, the variables I am setting to NULL are being set as "blank" i.e. value being updated is "" in the database.
    Whereas I want the value in the DB to remain as NULL(the column is defined as NULL with the default value)

    I would appreciate any insight onto this.


    Thanks in Advance
     
    rvaibhav, Jan 23, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    You would do that at the form level, something as simple as an if empty statement after your $_POST values are collected will do
    
    if (empty($name)){
        $name = NULL;
        }
    
    PHP:
    But then again, name is something you might not what left blank ?

    
    if (empty($name)){
        echo "NAME must not be empty";
        }
    
    PHP:
    Try a google search for Form Validation to find the best method that suits your needs.
     
    MyVodaFone, Jan 24, 2011 IP
  3. rvaibhav

    rvaibhav Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This PHP code I have already included just before updating in the database.

    and btw its not necessary that the user populates all the fields in the form. He may populate only 1 field out of the 7 mentioned in the main post.
     
    rvaibhav, Jan 24, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    I think the column needs to be set as text and then $name = "NULL"; might work ?
     
    MyVodaFone, Jan 24, 2011 IP
  5. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #5
    nope, MyVodaFone it won't work, your first method should work.

    However, why do you want to set it as NULL and just not to left it blank?
     
    G3n3s!s, Jan 24, 2011 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Your wrapping quotes around the variable, that should only be neccesary if the $variable contains data which is not NULL...if its NULL don't wrap it within quotes (otherwise it will be parsed as a string rather then literally).

    e.g.

    $variable = empty($variable) ? NULL : "'{$variable}'";
    
    $sql = "UPDATE table SET column_name = {$variable}";
    PHP:
     
    danx10, Jan 25, 2011 IP