我想通过在php下拉菜单上的过滤器来显示sql表


i want to display msql table by puting the filter on drop down menu in php?

因为下面的代码直接访问数据库表,但我希望它能在下拉菜单中显示表内容,就像我在一个下拉菜单中选择伊斯兰堡和在其他代码中给出的拉合尔一样,并按搜索按钮,然后它显示表航班。但是它直接显示

<p class="h2">Quick Search</p>
    <div class="sb2_opts">
     <p>
   </p>
<form method="post" action="haseeb.php">
 <p>Enter your source and destination.</p>
<p>
    From:</p>
<select name="from">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<p>
    To:</p>
   <select name="To">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<input type="submit" value="search" /> 
</form>
</form> </table>
<?php

$db_host = 'localhost';
$db_user = 'root';

$database = 'homedb';
$table = 'flights';
if (!mysql_connect($db_host, $db_user))
    die("Can't connect to database");
if (!mysql_select_db($database))
    die("Can't select database");
$whereClauses = array(); 
if (! empty($_POST['from'])) $whereClauses[] ="from='".mysql_real_escape_string($_POST['from'])."'"; 
if (! empty($_POST['To'])) $whereClauses[] ="To='".mysql_real_escape_string($_POST['To'])."'"; 
$where = ''; 
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } 

$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");

}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";

while($row = mysql_fetch_row($result))
{
    echo "<tr>";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";
    echo "</tr>'n";
}

mysqli_close($con);
?>

在提交表单之前显示表的原因是因为您将表单定向到同一页面,而没有使用条件语句验证它们。尝试在if验证中编码输出到屏幕的php代码。

if($_POST)
{
   echo "<h1>Table: {$table}</h1>";
   echo "<table border='1'><tr>";
   while($row = mysql_fetch_row($result))
   {
      echo "<tr>";
      foreach($row as $cell)
          echo "<td>$cell</td>";
      echo "</tr>'n";
   }
}