Note that there are some explanatory texts on larger screens.

plurals
  1. POUse dropdown list selection as mySQL query parameter
    text
    copied!<p>I had asked a similar question a few days ago but think I was trying to do to much at one time. I am hoping someone can help get me started on this.</p> <p>I have two drop down lists, one will be populated with years (2012, 2011 etc) and I have some mySQL databases called "db_2012", "db_2011" etc. In these databases are tables representing months.</p> <p>I would like the user to select a year and then use that selection to query the correct db and return a list of table names which will be used to populate the second drop down list. Then click a button "See Results" to query the selected table and show the results.</p> <p>I am putting this on a wordpress website and am using a php template file that I created. This is still new to me and what I have so far doesnt work like I want it too, it is basically setup now that you select a year and select a month (not populated from db) and click a button. It makes the query and my table is displayed, but I need this solution to be more dynamic and work as described above. Thanks for the help.</p> <pre><code>echo '&lt;form action="" method="post"&gt;'; echo'&lt;select name="years" id="years"&gt;'; foreach($yearList as $year){ echo'&lt;option value="'.$year.'"&gt;'.$year.'&lt;/option&gt;'; } echo'&lt;/select&gt;&lt;br /&gt;'; echo '&lt;select name="monthList" id="months"&gt;'; foreach($monthList as $month) { echo'&lt;option value="'.$month.'"&gt;'.$month.'&lt;/option&gt;'; } echo '&lt;/select&gt;'; echo '&lt;input type=\'submit\' value=\'See Results\'&gt;'; echo '&lt;/form'&gt; </code></pre> <p>$yearList and $monthList are just pre populated arrays. So now from here I want to click the See Results button and query my sql database using the parameters from the drop down selections.</p> <pre><code>$database = $_POST['yearList']; $month = $_POST['monthList']; $wpdbtest_otherdb = new wpdb('Username', 'Password', $database, 'localhost'); $qStr = "SELECT * FROM $month"; $myResults = $wpdbtest_otherdb-&gt;get_results($qStr, OBJECT); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload