Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>see below for a revision to your code in order to achieve what you're looking for.</p> <pre><code>&lt;form method='post' action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;"&gt; &lt;?php define('PDO_USER', 'root'); define('PDO_PASS', ''); function getDatabases(PDO $pdo) { $stmt = $pdo-&gt;query('SHOW DATABASES'); $result = $stmt-&gt;fetchAll(); $dbs = array(); foreach($result as $row) { $dbs[] = $row['Database']; } return $dbs; } $pdo = new PDO('mysql:host=localhost', PDO_USER, PDO_PASS); $pdo-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $databases = getDatabases($pdo); // Table code $selectedDB = (!empty($_POST['database'])) ? $_POST['database'] : null; function getTables(PDO $pdo, $databaseName) { if(!in_array($databaseName, getDatabases($pdo))) { return array(); } $stmt = $pdo-&gt;query('SHOW TABLES FROM '.$databaseName); $result = $stmt-&gt;fetchAll(); $tables = array(); foreach($result as $row) { $tables[] = $row['Tables_in_'.$databaseName]; } return $tables; } $tables = array(); if(!empty($selectedDB)) { $tables = getTables($pdo, $selectedDB); } ?&gt; Database: &lt;select name='database'&gt; &lt;?php foreach($databases as $row): ?&gt; &lt;option value="&lt;?php echo $row; ?&gt;"&gt;&lt;?php echo $row; ?&gt;&lt;/option&gt; &lt;?php endforeach; ?&gt; &lt;/select&gt;&amp;#160; &lt;input type='submit' name='formSubmit' value='Submit'&gt;&lt;/form&gt; UPDATE &lt;select name='formTable1'&gt; &lt;?php foreach($tables as $tbName): ?&gt; &lt;option value='&lt;?php echo $tbName; ?&gt;'&gt;&lt;?php echo $tbName; ?&gt;&lt;/option&gt; &lt;?php endforeach; ?&gt; &lt;/select&gt; </code></pre> <p>A couple of notes:</p> <ol> <li><p>You need to include the "action" attribute in the form tag in order to give the form a target URL for submission.</p></li> <li><p>In your original select you had 2 name attributes (name='formDatabases[]' name='database'). You should only have one name per form element, which is what is determined to be used as the key in the $_POST array for that particular field.</p></li> <li><p>You should only use "name='var[]'" format in the HTML, if you expect that particular POST item to be an array in PHP. Otherwise you can just use "name='var'"</p></li> <li>This may be out of the scope of your original question, but you may want to consider separating your database business logic into a separate class, to keep your HTML clean. For now at least, I updated the code to have the logic at the top of the file so that in can be extracted into a class more easily in the future. This avoids the perilous path into PHP Spaghetti string code!</li> </ol>
 

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