SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to insert NULL

    OK, I have some PHP/MySQL experience but not enough to say I am an expert - or even an advanced intermediate.

    I have a routine that gets the input from a form and attempts to put it into a database. However, I cannot figure out how to send NULL for the time - and, yes, the DB has a default of NULL and allows NULL.

    So, I have an array called $values that stores the various form input. One input is called 'eventStarttime' and can be null. I then build the query format using the code below. However, I cannot get the NULL that is put in the $values['eventStarttime'] variable to actually be put int the db. I get 00:00:00 in the db.

    Please let me know how I can get the NULL to be inserted into my db.

    Code:
      $myQuery = "INSERT INTO Events ";
     $fieldNames ="(";
     $fieldValues ="(";
     foreach( $values as $key => $whatsin){
        if (trim($key) != 'task')
        {
          $fieldNames .= $key.",";
          $fieldValues .= "'".$whatsin."',";
        }
        
    }
    $myQuery .= $fieldNames."eventActive, eventSpecial, eventInputDate) VALUES ".$fieldValues."'1','0','".date('Y-m-d')."')";

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    00:00:00 is the value inserted for a non-type-valid entry.

    Send a literal NULL (no quotes) for the value.

    (Note: It will still represent it as 00:00:00 when retrieved.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •