PHP / MySQL搜索查询返回空白表单

时间:2015-12-30 17:02:32

标签: php mysql search

当我搜索时,我得到一个结果,但它返回一个空白表格。我能够完成一次没有错误的搜索,但是当完成搜索时,HTML就是空的。我无法弄清楚这里有什么问题。 MySQL连接已被更改,原因很明显这是我的代码:

<?php
    define('DB_NAME', 'name');
    define('DB_USER', 'user');
    define('DB_PASS', 'password');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);

    if (!$link) {
        dir('There was a problem when trying to connect to the host. Error: ' . mysql_error());    
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$link) {
        dir('There was a problem when trying to connect to the database. Error: ' . mysql_error());    
    }

    $output = ""; 

    if (isset($_POST['search'])) {
        $searchq = $_POST['search'];

        $query = mysql_query("SELECT * FROM fac WHERE firstname LIKE '%$searchq%' OR lastname LIKE '%$searchq%'") or die("Could not complete search");
        $count = mysql_num_rows($query); 
        if ($count == 0) {
            $output = 'There where no search results';
        }else {             
            while ($row = mysql_fetch_array($query)) {
                $fname = $_POST['firstname'];
                $mname = $_POST['middlename'];
                $lname = $_POST['lastname'];
                $address = $_POST['address'];
                $car_type = $_POST['car_type'];
                $plate_number = $_POST['plate_number'];
                $intel = $_POST['intel'];

                $output .='<div> ID:'.$id.' <br /> First Name: </> '.$fname.' <br />  Middle Name: </>'.$mname.' <br />  Last Name: </> '.$lname.' <br />  Address: </> '.$address.' <br />  Car Type: </>'.$car_type.'<br />  Plate Number: </> '.$plate_number.' <br />  Intel: </> '.$intel.' <br />  </h3> </div>  <br />';
            }
        }       
    } 
?>
<html> 
    <head>
        <title>Search</title>
    </head>
    <body>
        <form action="search-entry-fac.php" method="post">
            <input type="text" name="search" placeholder="Search For Entry">
            <input type="submit" name="search_submit" value=">>"
        </form>
        <br />
        <?php print("$output"); ?>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

在您的代码中,您使用的是$_POST,而不是 $row

应该是这样的:

while ($row = mysql_fetch_array($query)) {
    $fname = $row['firstname'];
    $mname = $row['middlename'];
    $lname = $row['lastname'];
    $address = $row['address'];
    $car_type = $row['car_type'];
    $plate_number = $row['plate_number'];
    $intel = $row['intel'];
您正在使用名为$output .='<div> ID:'.$id.' <br /> [...]'的变量

旁注:,没有$id,因此我假设您遗失$id = SOMETHING; 1}}。

<小时/> 注2:要查看此类型的错误,您可以查看How do I get PHP Errors to display?

注3: mysql_* functions are deprecated and should be avoided。由于这可能是用户输入,请查看How can I prevent SQL-injection in PHP?