我想从数据库中选择一个表并显示在下拉列表中

时间:2014-10-09 03:56:17

标签: php html mysql

我想从特定数据库中选择一个表,然后表的列表将出现在select标签下的选项标签中。

非常感谢。

当前代码:

<?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>

4 个答案:

答案 0 :(得分:2)

问题是您已经提取行但尚未提交表单。

我建议用这种方式重构逻辑:

$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>

旁注:如果您已打开错误报告,这应该会对您做错的事情有所了解。请开启它。

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

答案 1 :(得分:0)

<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>

答案 2 :(得分:0)

试试这个

<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>

答案 3 :(得分:0)

考虑以下代码:

 <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>