我想选择一个表并使用我选择的表中的相同数据创建一个表

时间:2014-10-10 06:47:41

标签: php html mysql select create-table

这些是我的问题:

  1. 我从两个下拉列表中选择的值将更改回其默认值 当浏览器在表单中实现onchange函数时。

  2. 我想创建一个表,数据应该与之相同 我从第二个下拉列表中选择的表格。

  3. 这是我的代码

    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 CODE

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

1 个答案:

答案 0 :(得分:0)

如果发布了一些值且值等于循环值

,则在生成选项时添加检查foreach循环
  <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>