搜索功能不显示结果。

时间:2013-04-26 01:09:19

标签: php sql search

我开发了一个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 个答案:

答案 0 :(得分: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";

?>

答案 1 :(得分:0)

您应该将搜索代码放入一个仅在搜索字词包含值时才会运行的块:

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