所以我一直在研究一个程序,该程序从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用于绑定参数的语句,但不起作用。我不知道这里到底是什么错误。谢谢!
尝试为什么要OP “试试这个”?一个好的答案***将总是解释所做的事情以及为什么这样做,不仅是为了OP,还是为了将来访问SO。 –