【问题标题】:I want to select a table and create a table with the same data from the table i selected我想选择一个表并使用我选择的表中的相同数据创建一个表
【发布时间】:2014-10-10 06:47:40
【问题描述】:

这些是我的问题:

  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>

【问题讨论】:

  • 创建表 mytable 作为选择...

标签: php html mysql select create-table


【解决方案1】:

如果发布了一些值并且值等于循环值,则在生成选项时添加检查 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>

【讨论】:

  • 这不会输出表值。它只匹配来自 POST 和表值的值。如果值匹配,它将在 OPTION 标记中添加 SELECTED 属性。
  • 试着检查你的代码先生.. 因为当我实现它时,它只是在一个选项中给出两个值
  • 检查我编辑的代码。一个选项中不可能有两个值
猜你喜欢
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多