2014-10-10 62 views
0

這是我的問題:我要選擇一個表,並創建一個表,從表中的相同數據,我選擇

  1. 我從兩個選擇的值下拉列表中就會變回其默認值爲 當瀏覽器在窗體中實現函數onchange時。

  2. 我想創建一個表格,數據應該與我從第二個下拉列表中選擇的表格 相同。

這是我的代碼

PHP

<?php 
    $connectDatabase = mysql_connect("localhost","root","") or die(mysql_error()); 
    $tables = array(); 
    if(isset($_POST['select_db'])) 
    { // if its submitted 
     $select_db = $_POST['select_db']; 
     $mysql_select_db = mysql_select_db($select_db,$connectDatabase); 
     $drop_table = mysql_query("DROP TABLE pdf_table",$connectDatabase); 
     $query = "SHOW TABLES FROM $select_db"; 
     $mysql_query = mysql_query($query,$connectDatabase); 
     while($row =mysql_fetch_assoc($mysql_query)) 
     { 
      $tables[] = $row['Tables_in_' . $select_db]; // use associative instead 
     } 
    } 
    if(isset($_POST['select_table'])) 
    { 
     $select_table = $_POST['select_table']; 
     $db = mysql_select_db($select_db,$connectDatabase); 
     $query_select = "Create Table pdf_table AS (SELECT * FROM $select_table)"; 
     $select_query = mysql_query($query_select,$connectDatabase); 
    } 
    ?> 

HTML代碼

<form class="Search_Form" action="moduleindex.php" method="POST"> 
     <select name="select_db" onchange="this.form.submit();"> 
      <option disabled selected>Select Database</option> 
      <option>section_masterfile</option> 
     </select> 
     <select onchange="this.form.submit();" name="select_table"> 
      <option disabled selected>Select Table</option> 
      <?php foreach($tables as $table): ?> 
      <option value="<?php echo $table; ?>"><?php echo $table; ?></option> 
      <?php endforeach; ?> 
     </select> 
</form> 
+0

創建表mytable的AS SELECT ... – Strawberry 2014-10-10 06:58:13

回答

0

添加支票foreach循環而產生的選項,如果某個值發佈和價值等於t Ø循環值

<select onchange="this.form.submit();" name="select_table"> 
       <option disabled selected>Select Table</option> 
       <?php foreach($tables as $table){?> 
       <?php 
       $selected =""; 
       if(isset($_POST['select_table']) && $_POST['select_table'] == $table){ 

         $selected = 'selected="selected"'; 


        } ?> 
       <option value="<?php echo $table; ?>" <?php echo $selected ;?> ><?php echo $table; ?></option> 
       <?php }?> 
      </select> 
+0

我覺得你的代碼將輸出一個選項標記表的兩個值... – Jeff 2014-10-10 07:35:02

+0

這不會輸出表value.it只匹配值來自何處POST和表值。如果值匹配,它將在OPTION標記中添加SELECTED屬性。 – Kiran 2014-10-10 07:43:22

+0

嘗試檢查您的代碼先生..因爲當我實施它,它只是在一個選項內給出兩個值 – Jeff 2014-10-10 07:51:00

相關問題