使用php过滤数据库搜索

时间:2015-03-27 16:07:08

标签: php mysql database filter

我的数据库中名为“books”的表具有以下五个属性:State,Area,Institute,Category,Subject。我想使用HTML表单进行过滤搜索,用户可以选择最多5个过滤器并提交表单以从“书籍”表中查找书籍。

$sqltest="SELECT DISTINCT state FROM books order by state";
    $ex=$conn->prepare($sqltest);
    $ex->execute();
    echo "<select class='filter' name='state' onchange='showUser(this.value)'>";
    echo "<option value=' ' default selected>All State</option>";
    while($result = $ex->fetch(PDO::FETCH_ASSOC))
    {
        echo "<option value='".$result["state"]."'>".$result["state"]."</option>";

    }
    echo "</select>";

所有其他属性同样从数据库中获取其值。 我打算用'is not null'替换变量值,但我不认为它会解决问题。还有其他方法吗?我该怎么办?

if($state==" ")
        $state="is not null";

    if($institute==" ")
        $institute="is not null";

    if($location==" ")
        $location="is not null";

    if($subject==" ")
        $subject="is not null";

    if($category==" ")
        $category="is not null";

0 个答案:

没有答案