使用表单进行数据库搜索

时间:2015-11-20 16:00:11

标签: php

我正在尝试使用简单的表单来运行数据库搜索。我没有收到数据库连接错误,所以我很确定这不是问题所在。我检查了目标页面的回显显示,但我没有收到任何输出。

<!DOCTYPE HTML>
<html lang = "en">
  <head>
        <title>Tech Order Department.html</title>
    <meta charset = "UTF-8" />
</head>  
   <body> 

     <h3>Search  Contacts Details</h3>  
    <p>You  may search either by first or last name</p>  
     <form  method="post" action="Search.php"  id="searchform">  
     <input  type="text" name="name">  
       <input  type="submit" name="submit" value="Search">  
    </form> 

<?php  
   if(isset($_POST['submit'])){  
   if(isset($_GET['go'])){  
   if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){  
   $name=$_POST['name'];  
   //connect  to the database  
   $servername = "localhost";
   $username = "XXXXXXXXX"; // removed
   $password = "XXXXXXXXX"; // removed
   $dbname = "oldga740_SeniorProject";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 
   //-select  the database to use  
   $mydb=mysql_select_db("oldga740_SeniorProject");  
   //-query  the database table  
   $sql="SELECT  ID, FirstName, LastName FROM Staff WHERE FirstName LIKE '%" . $name .  "%' OR LastName LIKE '%" . $name ."%'";  
   //-run  the query against the mysql query function  
   $result=mysql_query($sql);  
   //-create  while loop and loop through result set  
   while($row=mysql_fetch_array($result)){  
           $FirstName  =$row['FirstName'];  
           $LastName=$row['LastName'];  
           $ID=$row['ID'];  
   //-display the result of the array  
   echo "<ul>\n";  
   echo "<li>" . "<a  href=\"Search.php\">"   .$FirstName . " " . $LastName .  "</a></li>\n";  
   echo "</ul>";  
   }  
   }  
   else{  
   echo  "<p>Please enter a search query</p>";  
   }  
   }  
   }  
 ?>


  </body>   
</html>  

0 个答案:

没有答案