I'm trying to let a user query a database by using combo boxes. The first combo box shows a list of tables in the database. When the user selects a table another query is ran which should populate the second combo box with the column names within that table (this will be used as a select for a query) I have the first combo box working but i am unable to get the second one to populate. Can anyone help with this? Many Thanks
here is my html code
<form id="ownQueryForm" name="ownQueryForm" method="POST" action="">
<div id = "tableSelect_div">
<P> Select table to Query: </P>
<select name = "Tables" id = "Tables" onchange = 'this.form.submit()'>
<option value = "blank"> Please choose a Table</option>
<?php foreach (($tableContent) as $row) : ?>
<option value = "<?php echo $row['Tables_in_DB_EXECUTION_MANAGER_TEST']; ?>"><?php echo $row['Tables_in_DB_EXECUTION_MANAGER_TEST'];?></option>
<?php endforeach ?>
</select>
</div>
<div id = "columnSelect_div">
<p>SELECT</p>
<select name = "Columns" id = "Columns">
<?php foreach (($columnContent) as $row) : ?>
<option value = "<?php echo $row['COLUMN_NAME']; ?>"><?php echo $row['COLUMN_NAME'];?></option>
<?php endforeach ?>
</select>
</div>
</form>
and here is my two queries so far
public static function getTables(){
global $db;
$st = $db->prepare("show tables");
$st ->execute();
$table = $st->fetchAll(PDO::FETCH_ASSOC);
return $table;
}
public static function getColumns(){
global $db;
$st = $db->prepare("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :value ");
$st->bindParam(':value', $_POST['Tables'], PDO::PARAM_STR);
$st -> execute();
$column = $st -> fetchAll(PDO::FETCH_ASSOC);
return $column;
//print_r($column);
}
}
I am using a controller that is used to render the getTable (which is in a model) and also to getColumns as two seperate functions on the same controller. can this be done?
I have this in the controller but it never gets run (unless i take out the if statement) and then it runs when the page loads
if (isset ( $_POST ['Tables'] )) {
$c = new ownQueryController();
$c ->queryRequest();
}
You could use:
'DESCRIBE ' . $_POST[ 'Tables' ]
Rather than
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :value "
This should return a array of columns with their name, type, limit etc. You can then loop though each column and display it.
WARNING
You will need to make sure your POST data is safe to put into the query. As @andy suggested your table could potentially be dropped if you just use this.