title: insert null date / time into mysql with php causing 0000-00-00 / 00:00:00
body: <p>i see a couple of questions on here and elsewhere that address this in some cases, but not quite what i need. </p>
  
  <p>i am working with a script that stores an array of values to do either an insert or update based on changes. </p>
  
  <p>some dates/times are set and some are null. i am trying with $var=NULL, then inserting with insert into ... ('{$var}') and getting 00:00:00 for the time if null and the time if time is set. </p>
  
  <p>inserting with $var="NULL" and insert into ... ('{$var}') and getting 00:00:00 if null and the time if time is set. </p>
  
  <p>if i remove the quotes for the insert ... ({$var}) it works if null, but if the date is not null, it of course fails. </p>
  
  <p>i have another VARCHAR field i am doing the same thing with and it works fine. i.e. if there is a string it is passed in as a string and if it's empty i am setting that var=null. i am using quotes on the insert query and if it's null, it goes in as null and if not null, the string inserts. </p>
  
  <p>using a conditional statement is going to require a pretty big rewrite, so i am hoping to avoid that. </p>
  
  <p>any suggestions on how to make this work without IF statements (if possible), would help out. </p>
  
  <p>thanks. </p>
  
  <hr>
  
  <p>i have tested this several ways and i am not able to get the desired results without making the date/time fields VARCHAR, which i don't want to do. here is the db structure, insert queries and results. </p>
  
  <pre><code>id  int(11)         No  None    AUTO_INCREMENT                          
  event_new_date_start    date            Yes NULL                                
  event_new_time_start    time            Yes NULL                                
  event_link  varchar(150)    latin1_swedish_ci       Yes NULL    
  
  
  
  
      $event_new_date_start1 = 'NULL';
      $event_new_time_start1 = 'NULL';
      $event_link1 = 'NULL';
      $event_new_date_start2 = '2011-04-04';
      $event_new_time_start2 = '13:13';
      $event_link2 = 'http://abc.com';
      $event_new_date_start3 = NULL;
      $event_new_time_start3 = NULL;
      $event_link3 = NULL;
  
      mysql_query("
      insert into test_dates (event_new_date_start, event_new_time_start, event_link) values
      ('{$event_new_date_start1}', '{$event_new_time_start1}', '{$event_link1}')
      ");
  
      mysql_query("
      insert into test_dates (event_new_date_start, event_new_time_start, event_link) values
      ('{$event_new_date_start2}', '{$event_new_time_start2}', '{$event_link2}')
      ");
  
      mysql_query("
      insert into test_dates (event_new_date_start, event_new_time_start, event_link) values
      ('{$event_new_date_start3}', '{$event_new_time_start3}', '{$event_link3}')
      ");                          
  
  
          1   0000-00-00  00:00:00    NULL
          2   2011-04-04  13:13:00    http://abc.com
          3   0000-00-00  00:00:00
  </code></pre>
  
  <p>when i changed the date/times fields to CHAR, it worked as expected using quotes in the query. </p>
  
  <pre><code>        4   NULL    NULL    NULL
          5   2011-04-04  13:13   http://abc.com
          6            
  </code></pre>