PHP SQL搜索页面返回错误

时间:2013-12-13 23:35:37

标签: php html sql

好的伙计们,也许我可以为这个最新的问题找到一些明确的答案。这个PHP部分没有返回任何响应,但是,我知道数据存在。有谁知道为什么这会返回文本“错误”?

<html>
<head>
<title>Search</title>
<style type="text/css">
    table {
        background-color: #FCF;
    }

    th {
        width: 150px;
        text-align: left;
    }
</style>
</head>

<body>
<h1>Search</h1>


<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true"/>

<label> Search | Category:
    <select name="category">
        <option value="fname">FName</option>
        <option value="lname">LName</option>
    </select>
</label>

<label>Search Criteria: <input type="text" name="criteria"/></label>
<input type="submit"/>
</form>

<?php

if (isset($_POST['submitted'])) {

// connect to DB
include('connect.php');

$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM people WHERE category = '$category'";
$result = mysqli_query($dbcon, $query) or die ('Error');


echo "<table>";
    echo "<thead>";
        echo "<tr>";
            echo "<th>First Name</th>";
            echo "<th>Last Name</th>";
        echo "</tr>";
    echo "</thead>";
    echo "<tbody>";
        if (mysqli_num_rows($result)) {
            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                echo "<tr>";
                    echo "<th>" . $row['fname'] . "</th>";
                    echo "<th>" . $row['lname'] . "</th>";
                echo "</tr>";
            }
        }
    echo "</tbody>";
echo "</table>";
}  
?>
</body>
</html>

1 个答案:

答案 0 :(得分:5)

可能是因为这句话:$result = mysqli_query($dbcon, $query) or die ('Error');
如果查询失败,脚本将停止并打印“错误” 您可能希望添加mysqli_error($dbcon)而不是'Error'之类的内容,在这种特定情况下,我建议回显查询以查看其外观。

请在数据库查询中使用它之前转义POST数据,或者使用预准备语句!