PHP从下拉列表中检索表

时间:2014-05-29 03:37:22

标签: php mysql sql

无论如何根据下拉列表检索整个表格?就像当我选择一个并按提交时它将带来那个特定的表? 'gameResults'是具有表格的数据库。我的代码如下:

if(isset($_POST["submit"])) {

 $tablesnames = $_POSt["tablenames"]; // name of selection list

 if($_POST["tablenames"] == '1') {    // if option 1 is selected

  // display table

 } }

 <?php 

  $conn = mysql_connect("localhost", "xxxxxx", "xxxxxx");
  mysql_select_db("gameResults", $conn)
  or die ('Database not found ' . mysql_error() );
  "SELECT TABLE FROM information_schema.tables WHERE table_schema = 'gameResults'";
  $rs = mysql_query($sql, $conn)
  or die ('Problem with query' . mysql_error());

  ?>


 <html>
 <body>
 <form name ="tables"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

  <p>Choose form to display :
  <select name="tablesnames" id="tablesnames">
<option value="nothing"> </option>
<option value="fixtures"> Fixtures </option>
<option value="results"> Results </option>
<option value="teams"> Teams </option>
<option value="seasons"> Seasons </option>
<option value="administrators"> Administrators </option>
<option value="users"> Users </option>
   </select></p>

   <input type="submit" name="submit" > <input type="reset"
   <html>
   </body>

1 个答案:

答案 0 :(得分:0)

首先,没有列TABLE。您要找的是TABLE_NAME。请考虑以下示例:(请停止使用mysql_个功能,改为使用PDOmysqli_

<?php

$tables = array();
// host, db user, db password, datababse
$con = mysqli_connect("localhost","test","test","test");
$query = mysqli_query($con, "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = 'test'");
$tables = mysqli_fetch_all($query, MYSQLI_ASSOC);

?>

<!-- loop thru the values -->
<select name="whatever">
<?php foreach($tables as $value): ?>
    <option value="<?php echo $value['TABLE_NAME']; ?>"><?php echo $value['TABLE_NAME']; ?></option>
<?php endforeach; ?>
</select>