2013-04-26 131 views
0

我開發了一個search function,它通過姓和名查找患者並顯示結果。但是,在執行PHP代碼後,搜索結果不顯示。搜索功能不顯示結果。

請注意:error消息不顯示;

有沒有人有任何想法,爲什麼它不顯示搜索結果?

<html> 
<h1>Search By Name</h1> 
<form action="" method="get"> 
    <label>Name: 
    <input type="text" name="keyname" /> 
    </label> 
    <input type="submit" value="submit" /> 
</form> 
</body> 
</html> 

<?php 

//capture search term and remove spaces at its both ends if there is any 

if(isset($_GET['submit'])){ 
if(!isset($_GET['keyname'])){ 
    $_GET['keyname'] = ""; 


$keyname = $_GET['keyname']; 
$searchTerm = trim($keyname); 

//check whether the name parsed is empty 
if($searchTerm == "") 
{ 
    echo "Enter name you are searching for."; 
exit(); 
} 

     //database connection info 
     $host = "localhost"; //server 
     $db = "a&e"; //database name 
     $user = "root"; //dabases user name 
     $pwd = ""; //password 

//connecting to server and creating link to database 
$link = mysqli_connect($host, $user, $pwd, $db); 

//MYSQL search statement 
$query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE 'Forename' = '$keyname' OR 'Surname' = '$keyname'"; 

$results = mysqli_query($link, $query); 

/* check whethere there were matching records in the table 
by counting the number of results returned */ 
if(mysqli_num_rows($results) >= 1) 
{ 
    $output = ""; 
    while($row = mysqli_fetch_array($results)) 
    { 
     $output .= "PatientID: " . $row['PatientID'] . "<br />"; 
     $output .= "Forename: " . $row['Forename'] . "<br />"; 
     $output .= "Surname: " . $row['Surname'] . "<br />"; 
     $output .= "Gender: " . $row['Gender'] . "<br />"; 
     $output .= "Illness: " . $row['Illness'] . "<br />"; 
     $output .= "Priority: " . $row['Priority'] . "<br />"; 
     $output .= "Patient History: " . $row['Patient_History'] . "<br /><br />"; 
    } 
    echo $output; 
} 
else { 
     echo "There was no matching record for the name " . $searchTerm; } 
     } 
     } 
?> 
+2

是您的MySQL與您的網絡服務器登錄? ..永遠不要使用root作爲你的數據庫用戶! – Daniel 2013-04-26 01:14:41

+0

爲什麼你在後面的代碼中仍然在'$ searchTerm'上使用'$ keyname'?還要在你的mysql函數週圍放一個try/catch塊,看看你是否遇到任何問題。也可以嘗試使用記錄器在某些點輸出變量以進行分析。 – SimonDever 2013-04-26 01:15:45

回答

0

試圖在您之前的問題上發佈這個。如果你想讓人們回答這些問題,你必須讓他們留下足夠長的時間讓人們回答。

<?php 
    $form = "<html> 
    <h1>Search By Name</h1> 
    <form method=\"get\"> 
     <label>Name: 
     <input type=\"text\" name=\"keyname\" /> 
     </label> 
     <input type=\"submit\" value=\"Search\" /> 
    </form> 
    </body> 
    </html>"; 

//capture search term and remove spaces at its both ends if there is any 

if(!empty($_GET['keyname'])){ 
    $keyname = $_GET['keyname']; 

    $searchTerm = trim($keyname); 

    //database connection info 
    $host = "localhost"; //server 
    $db = "a&e"; //database name 
    $user = "root"; //dabases user name 
    $pwd = ""; //password 

    //connecting to server and creating link to database 
    $link = mysqli_connect($host, $user, $pwd, $db); 

    //MYSQL search statement 
    $query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE Forename LIKE '%$searchTerm%' OR Surname LIKE '%$searchTerm%'"; 

    $results = mysqli_query($link, $query); 

    /* check whethere there were matching records in the table 
    by counting the number of results returned */ 
    if(mysqli_num_rows($results) >= 1){ 
     $output = ""; 
     while($row = mysqli_fetch_array($results)) 
     { 
      $output .= "PatientID: " . $row['PatientID'] . "<br />"; 
      $output .= "Forename: " . $row['Forename'] . "<br />"; 
      $output .= "Surname: " . $row['Surname'] . "<br />"; 
      $output .= "Gender: " . $row['Gender'] . "<br />"; 
      $output .= "Illness: " . $row['Illness'] . "<br />"; 
      $output .= "Priority: " . $row['Priority'] . "<br />"; 
      $output .= "Patient History: " . $row['Patient_History'] . "<br /><br />"; 
     } 
    }else{ 
     $output = "There was no matching record for the name " . strip_tags($searchTerm); 
    } 

} else { 
    $output = "Enter name you are searching for."; 
} 

    echo "$form\n$output"; 

?> 
0

你應該把你的搜索代碼到被運行只有當檢索詞有一個值的塊:

if(empty($searchTerm)) 
{ 
    echo "Enter name you are searching for."; 
} 
else 
{ 
    // run your search code here and display the result. 
}