Jeff
Jeff

Reputation: 19

I want to select a table from a database and show in a drop down list

I wanted to select a table from a specific database and then the list of tables will appear in an option tag under select tag.

Thanks a lot.

Current Code:

<?php

include 'database/connectdatabase.php';

if(isset($_POST['select_db']))
{
    $select_db = $_POST['select_db'];
    $selectdb_query = 'SHOW TABLES FROM $select_db';
    $query_select = mysql_query($selectdb_query,$connectDatabase);  

if(!$query_select)
{
    echo 'NO table selected!';
}

?>

<form method="POST" action="selecttable.php" autocomplete="off">
  <select name="select_db">
    <option  selected="selected">Select Database</option>
    <option>section_masterfile</option>
  </select>
</form>

<form method="POST" action="#" autocomplete="off">
  <?php while ($row = mysql_fetch_row($query_select)) {
    $num_row = mysql_num_rows($row);?>
    <select name="select_table">
        <option  selected="selected">Select Table</option>
            <?php for($i=0;$i>=$num_row;i++){?>
                <option><?php echo $row[0];?></option>
            <?php}?>
    </select>
  <?php}?>
</form>

Upvotes: 1

Views: 1421

Answers (4)

Kevin
Kevin

Reputation: 41885

The problem is that you're already fetching rows yet you haven't even submitted the form yet.

I suggest restructure you logic this way:

$con = new mysqli('localhost', 'username', 'password', 'database');
$tables = array();
if(isset($_POST['select_db'])) { // if its submitted
    $select_db = $con->real_escape_string($_POST['select_db']); // escape the string
    $query = $con->query("SHOW TABLES FROM $select_db");
    while($row = $query->fetch_assoc()) {
        $tables[] = $row['Tables_in_' . $select_db]; // use associative instead
    }
}


?>

<form method="POST" autocomplete="off">
    <select name="select_db" onchange="this.form.submit();">
        <option disabled selected>Select Database</option>
        <option>test</option>
    </select>
    <br/><br/>

    <select name="select_table">
        <?php foreach($tables as $table): ?>
            <option value="<?php echo $table; ?>"><?php echo $table; ?></option>
        <?php endforeach; ?>
    </select>
</form>

Sidenote: If you have turned on the error reporting, this should have given some red light to what you are doing wrong. Kindly turn it on.

error_reporting(E_ALL);
ini_set('display_errors', '1');

Upvotes: 2

Manwal
Manwal

Reputation: 23816

Consider this code:

 <form method="POST" action="#" autocomplete="off">

    <select name="select_table">
        <option  selected="selected">Select Table</option>
    <?php 
      while ($row = mysql_fetch_row($query_select)) {
    ?>

        <option><?php echo $row[0];?></option>

    <?php
    }
    ?>
  </select>
</form>

Upvotes: 0

Edwin Thomas
Edwin Thomas

Reputation: 1186

Try This

<form method="POST" action="#" autocomplete="off">

    <select name="select_table">
        <option  selected="selected">Select Table</option>
            <?php 
                while ($row = mysql_fetch_row($query_select)) 
                {
            ?>
                <option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
            <?php 
                } 
            ?>
    </select>

</form>

Upvotes: 0

John V
John V

Reputation: 875

<select name="select_table">
  <option  selected="selected">Select Table</option>
  <?php while ($row = mysql_fetch_row($query_select)) { ?>
      <option value = "<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
  <?php } ?>
</select>

Upvotes: 0

Related Questions