网站搜索显示整个表而不是搜索结果?

时间:2014-06-27 13:08:36

标签: php sql database mysqli

我有一个数据库,员工信息显示在一个表格中,我已设法创建搜索框,我已连接到我的数据库,现在问题是,当我搜索名称时,即" Daniel& #34;它只是让我回到了我已经拥有的桌子,所以有两张桌子而不是#34; Daniels"信息。

继承我的代码 -

<!doctype html>
  <html>
       <head>
          <title></title>
          <link rel="stylesheet" href="../Css/index_stylesheet.css">
       </head>
  <body>
    <p>

    <?php
        $form = "<html>
        <h1>Search!</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 = "development_suite"; //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  firstname ,surname,address,mobile,email
                        FROM development_suite.eeg_staff;";

            $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 = "$row_1";
                while($row = mysqli_fetch_array($results))
                {
                    $output .= "First Name: " . $row['firstname'] . "<br />";
                    $output .= "Surname: " . $row['surname'] . "<br />";
                    $output .= "Address: " . $row['address'] . "<br />";
                    $output .= "Mobile: " . $row['mobile'] . "<br />";
                    $output .= "Email: " . $row['email'] . "<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";
        ?>
     </body>
 </html>

1 个答案:

答案 0 :(得分:0)

您必须在查询中添加where子句。有点像...

$query = "SELECT  firstname ,surname,address,mobile,email
                    FROM development_suite.eeg_staff 
                    WHERE firstname LIKE '" . $_REQUEST['keyname'] . "';";

祝你好运, 内博伊沙