过滤功能仅使用一个参数

时间:2016-04-21 12:14:01

标签: php

所以我一直在研究一个程序,它从MySql数据库中获取信息,然后接受它并将其放入表中,并且它还能够过滤它,每当我输入一个参数时,它工作正常,但它不适用于两个或更多参数这里有我所拥有的,这部分创建MySQL查询语句

<?php   

                        require 'databaseconnect.php';
                        $filterstmt = ("SELECT * FROM Inventory");
                        if (!empty($_POST['ID'])):
                            $filterstmt .= (" WHERE ID = :id");

                        endif;
                        if (!empty($_POST['ItemCode'])):
                            $filterstmt .= (" WHERE Item = :code");

                        endif;
                        if (!empty($_POST['Type'])):
                            $filterstmt .= (" WHERE Type = :type");

                        endif;
                        if (!empty($_POST['Condition'])):
                            $filterstmt .= (" WHERE PartCondition = :condition");

                        endif;
                        if (!empty($_POST['Location'])):
                            $filterstmt .= (" WHERE Location = :loc");

                        endif;

                        $preparedfilterstmt = $conn->prepare($filterstmt);
                        if (!empty($_POST['ID'])):
                        $preparedfilterstmt->bindParam(':id', $_POST['ID']);
                        endif;
                        if (!empty($_POST['ItemCode'])):
                        $preparedfilterstmt->bindParam(':code', $_POST['ItemCode']);
                        endif;
                        if (!empty($_POST['Type'])):
                        $preparedfilterstmt->bindParam(':type', $_POST['Type']);
                        endif;
                        if (!empty($_POST['Condition'])):
                        $preparedfilterstmt->bindParam(':condition', $_POST['Condition']);
                        endif;
                        if (!empty($_POST['Location'])):
                        $preparedfilterstmt->bindParam(':loc', $_POST['Location']);
                        endif;

然后这部分执行预准备语句并创建表:

                        $preparedfilterstmt->execute();
                        $fltrtest = $preparedfilterstmt->rowCount();
                        if($fltrtest > 0):
                            echo ("<h3 class = 'Title'>Search Results: </h3>");

                    echo ("<table class = 'hubTable'> <tr class = 'tableheader'> <td class = 'hubCell'>ID</td> <td class = 'hubCell'>Item</td><td class = 'hubCell'>Type</td> <td class = 'hubCell'>Condition</td> <td class = 'hubCell'>Location</td> </tr> ");

                    while ($result = $preparedfilterstmt->fetch(PDO::FETCH_ASSOC)){
                    echo("<tr>"."<td class = 'hubCell'><a href = 'editinventroy.php?id=".$result['ID']."'>".$result['ID']."</a> </td>
                        <td class = 'hubCell'>".$result['Item']." </td>
                        <td class = 'hubCell'>".$result['Type']." </td>
                        <td class = 'hubCell'>".$result['PartCondition']." </td>
                        <td class = 'hubCell'>".$result['Location']." </td>

                         </tr>" );
                    }    
                    echo ("</table>");
                        else:
                            echo("<div class='alert alert-warning' role='alert'><b>Hmm...</b> Nothing seems to be under those parameters</div>");
                        endif;

我尝试使用try-catch而不是if语句来绑定参数,但这并不起作用。我不知道这里到底出了什么问题。谢谢!

2 个答案:

答案 0 :(得分:0)

你没有正确绑定param。 Query应始终拥有param wit and/or。所以你应该使用

$filterstmt = "SELECT * FROM Inventory WHERE";
$condition = "";
if (!empty($_POST['ID'])):
    $condition .= (" ID = :id");
endif;
if (!empty($_POST['ItemCode'])):
    if (empty($condition)) {
        $condition .= (" Item = :code");
    } else {
        $condition .= (" AND Item = :code"); //use AND/Or according to your query
    }

endif;
//remaining code

然后

$filterstmt = $filterstmt . $condition;

答案 1 :(得分:0)

试试这个

  $filterstmt = ("SELECT * FROM Inventory Where 1==1 ");
                    if (!empty($_POST['ID'])):
                        $filterstmt .= (" AND ID = :id");

                    endif;
                    if (!empty($_POST['ItemCode'])):
                        $filterstmt .= (" AND Item = :code");

                    endif;
                    if (!empty($_POST['Type'])):
                        $filterstmt .= (" AND Type = :type");

                    endif;
                    if (!empty($_POST['Condition'])):
                        $filterstmt .= (" AND PartCondition = :condition");

                    endif;
                    if (!empty($_POST['Location'])):
                        $filterstmt .= (" AND Location = :loc");

                    endif;