2014-10-09 60 views
1

我想从特定数据库中选择一个表格,然后表格列表将显示在选择标签下的选项标签中。我想从数据库中选择一个表格并在下拉列表中显示

非常感谢。

目前代码:

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

错误,如果有?检查他们?另外,你应该将这些空间放在一起'' - '' – 2014-10-09 03:57:53

回答

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

+1这是最好的答案。最新的也是最新的。 – Darren 2014-10-09 04:06:14

+1

@Darren yep似乎合乎逻辑地写了一个答案,因为其较短的代码反正 – Ghost 2014-10-09 04:09:59

+0

@Ghost谢谢你拯救我一生的一半(GAYISH MODE。) – Jeff 2014-10-09 04:51:07

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