title: Export MySQL table to CSV via PHP by checkbox selection
body: <p>I need to display a list of results form a survey on a PHP page then export them to a CSV file. The list also acts as a summary that can be clicked thorugh to the full result.</p>
  
  <p>I have all that sorted but now I need to have the CSV export by a check bx selection so that we dont need to download the entire databse each time just the ones we need.</p>
  
  <p>My code so far below.</p>
  
  <pre><code>&lt;div class="resultsList"&gt;
  
              &lt;h1&gt;PEEM Results List&lt;/h1&gt;
              &lt;a class="exportCSV" href="https://domain.com/downloadcsv.php"&gt;Export to CSV&lt;/a&gt;
              &lt;!-- Export CSV button --&gt;
              &lt;h3 class="resultsbydate"&gt;All results by date&lt;/h3&gt;
  
  
                  &lt;div class="resultsListHeader"&gt;
                      &lt;div class="clientidTab"&gt;Agency&lt;/div&gt;
                      &lt;div class="clientidTab"&gt;Family ID&lt;/div&gt;
                      &lt;div class="clientidName"&gt;Name&lt;/div&gt;
                      &lt;div class="clientidTab"&gt;Date&lt;/div&gt;
                      &lt;div class="clientidTab"&gt;&lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="entriesListMain"&gt;
                      &lt;?php
                      $connection = mysql_connect("localhost", "username", "password"); // Establishing Connection with Server
                      $db = mysql_select_db("database_name", $connection); // Selecting Database
                      //MySQL Query to read data
                      $query = mysql_query("select * from results ORDER BY peemdate DESC", $connection);
                      while ($row = mysql_fetch_array($query)) {
                      echo "&lt;div&gt;&lt;input type=\"checkbox\" name=\"xport\" value=\"export\"&gt;&lt;span&gt;{$row['client_id']}&lt;/span&gt; &lt;span&gt;{$row['family_id']}&lt;/span&gt; &lt;span&gt;{$row['firstname']} {$row['lastname']}&lt;/span&gt; &lt;span&gt;".date("d F Y", strtotime($row['peemdate']))."&lt;/span&gt;";
                      echo "&lt;span&gt;&lt;a class=\"parents-button\" href=\"peem-parent-repsonses.php?id={$row['survey_id']}\"&gt;&lt;strong&gt;Parent&amp;rsquo;s Review&lt;/strong&gt;&lt;/a&gt;&lt;/span&gt;";
                      echo "&lt;span&gt;&lt;a href=\"peem-repsonses.php?id={$row['survey_id']}\"&gt;&lt;strong&gt;View Results&lt;/strong&gt;&lt;/a&gt;&lt;/span&gt;";
                      echo "&lt;/div&gt;";
                      }
                      ?&gt;
                  &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;?php
          if (isset($_GET['id'])) {
          $id = $_GET['id'];
          $query1 = mysql_query("select * from results where survey_id=$id", $connection);
          while ($row1 = mysql_fetch_array($query1)) {
          ?&gt;
  
          &lt;?php
          }
          }
          ?&gt;
  
          &lt;?php
          mysql_close($connection); // Closing Connection with Server
          ?&gt;
  </code></pre>
  
  <p>And the downloadcsv.php</p>
  
  <pre><code>&lt;?php
  $conn = mysql_connect("localhost","username","password");
  mysql_select_db("databasename",$conn);
  
  $filename = "peem_results.csv";
  $fp = fopen('php://output', 'w');
  
  $query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='realwell_peemfinal' AND TABLE_NAME='results'";
  $result = mysql_query($query);
  while ($row = mysql_fetch_row($result)) {
      $header[] = $row[0];
  }   
  
  header('Content-type: application/csv');
  header('Content-Disposition: attachment; filename='.$filename);
  fputcsv($fp, $header);
  
  $query = "SELECT * FROM results";
  $result = mysql_query($query);
  while($row = mysql_fetch_row($result)) {
      fputcsv($fp, $row);
  }
  exit;
  ?&gt;
  </code></pre>
  
  <p>Any help with this would be great, cheers</p>
  
  <p>Updated with a screenshot of what I am trying to achieve 
  <a href="https://i.stack.imgur.com/6HHJT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6HHJT.jpg" alt="enter image description here"></a></p>