title: Export to CSV with PHP (between selected date range)
body: <p>I have export to csv using a link working correctly, though I would like to have a form where the user selects the "start date" and "end date" and everything between those dates is exported to CSV.</p>
  
  <p>So far I have:
  Form Submit</p>
  
  <pre><code>&lt;form id="export" name="export" method="post" action="export.php"&gt;
  To&lt;input type="date" id="date1" name="date1"&gt;&lt;br&gt;
  From&lt;input type="date" id="date2" name="date2"&gt;&lt;br&gt;
  &lt;input type="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Export.php</p>
  
  <pre><code>&lt;?php
  
  $date1 = $_POST['date1'];
  $date2 = $_POST['date2'];
  
  $host = 'localhost'; // MYSQL database host adress
  $db = 'chboard'; // MYSQL database name
  $user = ''; // Mysql Datbase user
  $pass = '"; // Mysql Datbase password
  // Connect to the database
  $link = mysql_connect($host, $user, $pass);
  mysql_select_db($db);
  
  require 'exportcsv.inc.php';
  
  $table="vacationrequests"; // this is the tablename that you want to export to csv from mysql.
  
  exportMysqlToCsv($table);
  
  ?&gt;
  </code></pre>
  
  <p>exportcsv.inc.php</p>
  
  <pre><code>&lt;?php
  
  
  function exportMysqlToCsv($table,$filename = 'export.csv')
  {
  $csv_terminated = "\n";
  $csv_separator = ",";
  $csv_enclosed = '"';
  $csv_escaped = "\\";
  $sql_query = "select * from $table WHERE $start &gt;= $date1 AND $end &lt;= $date2";
  
  // Gets the data from the database
  $result = mysql_query($sql_query);
  $fields_cnt = mysql_num_fields($result);
  
  
  $schema_insert = '';
  
  for ($i = 0; $i &lt; $fields_cnt; $i++)
  {
      $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped .       $csv_enclosed,
          stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
      $schema_insert .= $l;
      $schema_insert .= $csv_separator;
  } // end for
  
  $out = trim(substr($schema_insert, 0, -1));
  $out .= $csv_terminated;
  
  // Format the data
  while ($row = mysql_fetch_array($result))
  {
      $schema_insert = '';
      for ($j = 0; $j &lt; $fields_cnt; $j++)
      {
          if ($row[$j] == '0' || $row[$j] != '')
          {
  
              if ($csv_enclosed == '')
              {
                  $schema_insert .= $row[$j];
              } else
              {
                  $schema_insert .= $csv_enclosed .
                  str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
              }
          } else
          {
              $schema_insert .= '';
          }
  
          if ($j &lt; $fields_cnt - 1)
          {
              $schema_insert .= $csv_separator;
          }
      } // end for
  
      $out .= $schema_insert;
      $out .= $csv_terminated;
  } // end while
  
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Content-Length: " . strlen($out));
  // Output to browser with appropriate mime type, you choose ;)
  header("Content-type: text/x-csv");
  //header("Content-type: text/csv");
  //header("Content-type: application/csv");
  header("Content-Disposition: attachment; filename=$filename");
  echo $out;
  exit;
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>The output is a CSV file and in the first cell all I get is a &lt;.  Any help in pointing me in the correct direction to get this working would be great.</p>