SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    865
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Confused with setting variables to NULL

    My website reads a mySQL database and populates a form with all the data. One of the fields is populated with a DATETIME. If the user is new, their guideUpdated is null. When I make changes to the form and click save, it populates the guideUpdated field in the database to '0000-00-00 00:00:00'. I'm trying to fix this so if I leave the guideUpdated field empty on my form, then a NULL value goes into the database when the save button is clicked and an update is made. Here is the code I have right before the update is made:

    if ($guideUpdated == "0000-00-00 00:00:00" || $guideUpdated == "") {

    $guideUpdated = null;
    }

    However, this still doesn't work. Even though $guideUpdated is blank on the form, it still adds all of the zeros. Can someone point out what I'm missing here?

    Thanks!
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your coulm definition in the db table might be set to NOT NULL or have 0's as the default. If so set the column default to allow NULL.

    Also since an empty string ("") and NULL are not the same thing, your insert sql statement need to insert NULL for the date and not ""


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
  •