douduan9129 2014-10-10 06:47
浏览 55

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

These are my problems:

  1. The value I selected from the two drop down list will change back to its default value when the browser implements the function onchange in the form.

  2. I want to create a table and the data's should be the same with the the table I selected from the second drop down list.

This is my code

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条回答 默认 最新

  • dsuikgi9199 2014-10-10 07:04
    关注

    Add check in foreach loop while generating Options if some value posted and value is equal to loop value

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

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan