title: SELECT query for multiple checkbox in HTML (PHP, Postgresql)
body: <p>I need to make <strong>dynamic SQL</strong> query that returns several columns from DB, depending on what data was checked in checkbox html form. In this code it only returns <strong>last column of data</strong> that was checked, <strong>not all</strong>. I can only think of examining all combination with IF clause, but since in practice I have much more checkboxes then 3, I'm wondering if someone can suggest some dynamic solution.</p>
  
  <pre><code>&lt;?php
  // Connecting to a Postgresql DB
  ...
  // Values sent from HTML form
  $checked = $_POST["check_list"];
  $submit = $_POST["submit"];
  ?&gt;
  
  &lt;form action="" method="POST"&gt;  
  Choose:
  &lt;input type="checkbox" name="check_list[]" value="5"&gt; 5h    
  &lt;input type="checkbox" name="check_list[]" value="6"&gt; 6h    
  &lt;input type="checkbox" name="check_list[]" value="7"&gt; 7h    
  &lt;br&gt;&lt;br&gt;    
  &lt;input type="submit" name="submit"&gt;
  &lt;/form&gt;
  
   &lt;?php
  // Checking if something is checked
  // If it is do something
  if(isset($submit)){
      if(empty($checked)){
          echo "Please choose something";     
      } else {        
          foreach($checked as $selected){
              // Adding 0 in front of one dimensional numbers because of DB column name
              if ($selected &lt; 10){
                  $selected = "0$selected";
              }
              $db_colum_name = "a_b$selected";
  
              $query = "SELECT table1. name,
                               table1. latitude / CAST(10 AS FLOAT(6)) AS lat,
                               table1. longitude / CAST(10 AS FLOAT(6)) AS lng,
                               EXTRACT(YEAR FROM table2. date) AS year,
                               table2. $db_colum_name AS $selected
                       FROM table1
                       JOIN table2 
                       ON table1.id=table2.id
                       WHERE
                       name = 'New York'
                       AND
                       EXTRACT(YEAR FROM table2. date) = '2000'
                       ORDER BY date DESC";
  
              $result = pg_query($dbcon, $query) or die("Your error: {$query} &lt;br&gt;");
          }
  
          // Printing data
          ...
          // Closing DB connection
          pg_close ($dbcon);
  
      }
  }
  ?&gt;
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>Somekind of schema:</p>
  
  <p><em>table 1</em></p>
  
  <p>id | name    | latitude | longitude |<br>
  5  | New York|   40.7   | -74.00    |</p>
  
  <p><em>table 2</em></p>
  
  <p>id | date      | a_b05 | a_b06 | a_b07 |<br>
  5  |2000-03-02 |  100  |  105  |  200  |</p>