php代码没有使用wamp服务器从mysql数据库中获取数据

时间:2014-08-21 20:19:39

标签: php html mysql phpmyadmin wampserver

我想通过在下拉菜单中的每个不同选项中显示以下条件,在phpMyAdmin中显示数据库中的表,通过按搜索按钮显示数据库中的不同表。但它没有这样做。

<p class="h2">Quick Search</p>
    <div class="sb2_opts">
     <p></p>
<form method="post" action="" >
   <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
$con=mysqli_connect("localhost","root","","test"); 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}

if(isset($_POST['from']) and isset($_POST['To'])) {
    $from = $_POST['from'] ;
    $to = $_POST['To'] ;
    $table = array($from, $to);

    switch ($table) {
        case array ("Islamabad", "Lahore") :
           $result = mysqli_query($con,"SELECT * FROM flights");
           echo "</flights>";      //table name is flights
           break;
        case array ("Islamabad", "Murree") :
           $result = mysqli_query($con,"SELECT * FROM isb to murree");
          echo "</isb to murree>";     //table name isb to murree
          ;
          break;
       case array ("Islamabad", "Muzaffarabad") :
           $result = mysqli_query($con,"SELECT * FROM isb to muzz");
           echo "</isb to muzz>";
           break;
       //.....
       //......
       default:
          echo "Your choice is nor valid !!";
    }

}
mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:0)

您遇到的当前问题似乎是由您的表名引起的。当它们包含空格(或者是保留字,以数字开头等)时,需要在反引号中引用它们:

SELECT * FROM `isb to murree`

但是,在我继续之前,我可能会仔细研究数据库结构(和表名...),因为为每个目标组合设置一个不同的表并不能很好地扩展。您可以轻松地将所有组合存储在一个表格中(仅使用数字来标识机场),可能还有一些其他列可用于提供更多信息。