title: MySQL, how to insert null dates
body: <p>I am having trouble inserting null values into date fields into a MySQL table. </p>
  
  <p>Here is the insert query:</p>
  
  <pre><code>$query = 'INSERT INTO table (column_s1, column_s2, column_d1, column_d2)
  VALUES ("'.$string1.'", "'.$string2.'", '.$date1.', '.$date2.')';
  </code></pre>
  
  <p>Columns s1 and s2 take string values and d1 and d2 take dates.  When I run this query with only the string fields, there is no problem.</p>
  
  <p>The date values can be either set or null, so I have not included the quotation marks in the query, but have instead added them to the variable earlier on.  This is the php code I am using to set the date values:</p>
  
  <pre><code>if (empty($date1)){
      $date1 = NULL;
  }
  else{
      $date1part = explode("/",$date1);
      $date1 = '"'.$date1part[2].'/'.$date1part[1].'/'.$date1part[0].'"';
  }
  </code></pre>
  
  <p>When the date values are all set, the record is inserted correctly. However, when either of the dates is null, nothing is inserted.</p>
  
  <p>Why can't I just insert null values into MySQL like this?</p>