title: How to use checkboxes to retrieve specific data in a database
body: <p>Lately i have been working on a form with which a user can select a checkbox and as a result of the selection it shows the database information in a table.</p>
  
  <p>I browsed the stackoverflow questions and found a question that was almost the same, namely: <a href="https://stackoverflow.com/questions/22922796/retrieve-data-from-sql-database-and-display-in-tables-display-certain-data-acc">Retrieve data from sql database and display in tables - Display certain data according to checkboxes checked</a></p>
  
  <p>So with this information, and some other information from the web, i started to create my code. Though after completion i had several errors, namely my database fields were duplicate on output, the data that the field had isn't outputted and there were two warnings. After a lot of searching and editing/trying i couldn't find the right solution so hence me asking this question in here.   </p>
  
  <p>Before i display my code i first give some information about the checkboxes, database and table/fields (and a (small) note: it is a wordpress database).</p>
  
  <p>I have 1 database called xxx_wp1. THis database contains various (wordpress) table's but the table i want to retrieve information from is called: wp_participants_database.</p>
  
  <p>This table contains various columns (around 15). Though, for this testing example i used just 3 of the 15 columns named: authorss, research_source and research_title. I inserted some random information (3 rows) in these 3 columns. 
  The form i created has, kinda obviously, 3 checkboxes for each column (so 1 for authors, research source and title).</p>
  
  <p>Based on the previous link and some wordpress information i started to create my code for the selection, it is as follows: </p>
  
  <pre><code>&lt;form method="post"&gt;
  &lt;input type="checkbox" name="columns[]" value="1" /&gt;&lt;label for="Authors"&gt;Authors&lt;/label&gt;&lt;br /&gt;
  &lt;input type="checkbox" name="columns[]" value="2" /&gt;&lt;label for="Research Source"&gt;Research Source&lt;/label&gt;&lt;br /&gt;
  &lt;input type="checkbox" name="columns[]" value="3" /&gt;&lt;label for="Research Title"&gt;Research Title&lt;/label&gt;&lt;br /&gt;
  &lt;input type="submit" name="go" value="Submit"/&gt;
  &lt;/form&gt;
  
  &lt;?php
  
  
  $all = false;
  $column_names = array('1' =&gt; 'Authors', '2'=&gt;'Research Source', '3'=&gt;'Research Title');
  $column_entries = isset($_POST['columns']) ? $_POST['columns'] : array();
  $sql_columns = array();
  foreach($column_entries as $i) {
     if(array_key_exists($i, $column_names)) {
      $sql_columns[] = $column_names[$i];
     }
  }
  
  if (empty($sql_columns)) {
   $all = true;
   $sql_columns[] = "*";
  } else {
   $sql_columns[] = "authorss,research_source,research_title,";
  }
  global $wpdb;
  
  //DNI CHECKBOX + ALL
  $tmp = $wpdb-&gt;get_results( "SELECT ".implode(",", $sql_columns)." FROM wp_participants_database"); 
  
  $result = mysql_query($tmp);
  echo "&lt;table border='1' style='width:450px'&gt;
  &lt;tr&gt;
  &lt;th&gt;authorss&lt;/th&gt;
  &lt;th&gt;research_source&lt;/th&gt;
  &lt;th&gt;research_title&lt;/th&gt;";
  foreach($column_names as $k =&gt; $v) { 
    if($all || (is_array($column_entries) &amp;&amp; in_array($k, $column_entries)))
       echo "&lt;th&gt;$v&lt;/th&gt;";
  }
  echo "&lt;/tr&gt;";
  while( $row = mysql_fetch_assoc($result))
  {
      echo "&lt;tr&gt;";  
      echo "&lt;td&gt;" . $row['authorss'] . "&lt;/td&gt;";   
      echo "&lt;td&gt;" . $row['research_source'] . "&lt;/td&gt;";   
      echo "&lt;td&gt;" . $row['research_title'] . "&lt;/td&gt;";  
      foreach($column_names as $k =&gt; $v) { 
        if($all || (is_array($column_entries) &amp;&amp; in_array($k, $column_entries))) {
           echo "&lt;th&gt;".$row[$v]."&lt;/th&gt;";
         }
      }
      echo "&lt;/tr&gt;";
  }
  echo '&lt;/table&gt;';
  
  
  ?&gt;
  &lt;?php
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>As you can see, the quesry is a bit different because it has to connect to the Wordpress database (the global $wpdb and $wpdb->get_results). While typing this im thinking that this might also be part of the problem as this get_results is already getting results which i am also getting later on?</p>
  
  <p>Anyway, while testing this i get a few errors / misbehavior which i cant seem to figure out. </p>
  
  <p>The first errors are the following warnings: </p>
  
  <pre><code> - Warning: mysql_query() expects parameter 1 to be string, array given in /home/xxx/domains/mysite.nl/public_html/Recap/wp-content/themes/radiate/templates/pdb-search-new.php on line 32 --- which is this line of code: `$result = mysql_query($tmp);`
   - Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /home/xxx/domains/mysite.nl/public_html/Recap/wp-content/themes/radiate/templates/pdb-search-new.php on line 43 --- which is this line of code: `while( $row = mysql_fetch_assoc($result))`
  </code></pre>
  
  <p>The second problem is that all of the columns are echo'd TWICE even before submitting. So this would mean this line of code is being done no matter what: </p>
  
  <pre><code>if (empty($sql_columns)) {
   $all = true;
   $sql_columns[] = "*";
  } else {
   $sql_columns[] = "authorss,research_source,research_title,";
  }
  </code></pre>
  
  <p>When i check a option and press the submit button, the right column is being showed (so yay that works), though all the 3 columns are still being showed (no matter what) as well as the selected one, so i got this if i select the first option: 
  authorss   research_source  research_title  Authors (notice the first 3 are from my defined  while the last one is from my defined $column_names. </p>
  
  <p>The last problem is that the field values of the columns isn't being showed, the columns are just empty. </p>
  
  <p>So the question is of anybody could give me some pointers about what is going wrong.</p>
  
  <p>Thank you in advance!</p>
  
  <p>*****UPDATE*****</p>
  
  <p>I made some adjusting with the help of @Zeusarm</p>
  
  <p>So firstly i changed the warnings (among others the <code>$wpdb-&gt;get_results</code> is changed to <code>$wpdb-&gt;query</code>) and the warnings are all gone. For some reason i gave the <code>$array_names</code> just their front-end names (stupid me), so i changed it, like you suggested to the proper column names as they are in the database table. So field 1,2,3 became: <strong>authors, research_source and research_title.</strong> </p>
  
  <p>I also added the other changes. Although the errors are all gone, i still get all 3 the columns showed + 1 duplicate (depending on the number of checkboxes selected). Plus i still don't get the database data which is stored in the columns (for example: authorss has the following stored values in it: Barry, Henk and Nicolas.). </p>
  
  <p>The code now looks like (with the form left out as it remained the same): </p>
  
  <pre><code>&lt;?php
  $all = false;
  $column_names = array('1' =&gt; 'authorss', '2' =&gt; 'research_source', '3' =&gt; 'research_title');
  if(isset($_POST['columns'])){
      $column_entries = $_POST['columns'];
      $sql_columns = array();
      foreach($column_entries as $i) {
          if(array_key_exists($i, $column_names)) {
              $sql_columns[] = $column_names[$i];
          }
      }
      $sql_columns[] = "authorss";
      $sql_columns[] = "research_source";
      $sql_columns[] = "research_title";
  } else {
      $all = true;
      $sql_columns[] = "*";
  }
  global $wpdb;
  
  //DNI CHECKBOX + ALL
  $tmp = $wpdb-&gt;query( "SELECT ".implode(",", $sql_columns)." FROM wp_participants_database"); 
  
  $result = mysql_query($tmp);
  echo "&lt;table border='1' style='width:450px'&gt;
  &lt;tr&gt;
  &lt;th&gt;authorss&lt;/th&gt;
  &lt;th&gt;research_source&lt;/th&gt;
  &lt;th&gt;research_title&lt;/th&gt;";
  foreach($column_names as $k =&gt; $v) { 
    if($all || (is_array($column_entries) &amp;&amp; in_array($k, $column_entries)))
       echo "&lt;th&gt;$v&lt;/th&gt;";
  }
  echo "&lt;/tr&gt;";
  
  if($result!==false &amp;&amp; mysql_num_rows($result)&gt;0){
      while( $row = mysql_fetch_assoc($result)){
          echo "&lt;tr&gt;";  
          echo "&lt;td&gt;" . $row['authorss'] . "&lt;/td&gt;";   
      echo "&lt;td&gt;" . $row['research_source'] . "&lt;/td&gt;";   
          echo "&lt;td&gt;" . $row['research_title'] . "&lt;/td&gt;";  
          foreach($column_names as $k =&gt; $v) { 
              if($all || (is_array($column_entries) &amp;&amp; in_array($k, $column_entries))) {
                 echo "&lt;th&gt;".$row[$v]."&lt;/th&gt;";
              }
          }
          echo "&lt;/tr&gt;";
      }
      echo '&lt;/table&gt;';
  }  
  
  ?&gt;
  &lt;?php
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p>*****UPDATE 2*****</p>
  
  <p>So i changed the code and finally i am retrieving the data of the database! So i guess i should just work with the wordpress get_results for querying from now on. 
  Although i am retrieving the information i still have duplicates. When i go to the page, at first i have all 3 duplicates and the data retrieving is being output in the first 3 columns. When i select 1 checkbox option, the correct data of that checkbox is being displayed and the other data from the other checkboxes isn't (so that works). Though, for example when i only choose the authors checkbox, the data of authors is being displayed in the first authors checkbox and only 1 duplicate (namely 'authors') is being showed. Though when i click only the second checkbox, research source (column research_source) then the data of that column is only being showed (what is correct) BUT that data is being output in thew first authors column and again, 1 duplicate with the correct column name namely 'research_source'. </p>
  
  <p>But because a picture says more than a 1000 words, i added some images to clear it up. (sorry for the links to the pictures but missing 2 reputation to post pics directly)</p>
  
  <p><strong>The starting columns/page (untouched):</strong></p>
  
  <p><a href="https://i.stack.imgur.com/oZo14.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oZo14.png" alt="The starting columns/page (untouched)"></a></p>
  
  <p><strong>Only authors selected and submitted:</strong></p>
  
  <p>Left this one out as i can also only upload 2 links withh less than 10 rep...</p>
  
  <p><strong>Only Research Source selected and submitted:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/WEbV9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WEbV9.png" alt="enter image description here"></a></p>