带有mySQl的PHP:查询用户每次选择要填写的字段

时间:2012-12-18 17:15:42

标签: php mysql

第一次使用PHP并且第一次使用mySQL所以如果我发现任何错误,请指出它们。

我希望能够从我的表中获取不是问题的数据,我希望用户填写尽可能多的textBoxes或小文本框和返回与其搜索相关的所有数据?

我有这些变量

 //get variables from page before  
$ProductName = $_POST["ProductName"];  
$ProductCategory = $_POST["ProductCategory"]; 
$ProductAge = $_POST["ProductAge"]; 
$ProductDis = $_POST["ProductDis"]; 
$ProductPrice = $_POST["ProductPrice"];
$ProductInStock = $_POST["ProductInStock"];

并想要某种类型的查询(如果可能的话)

$query = "SELECT * FROM Product WHERE such and such ";

1 个答案:

答案 0 :(得分:1)

您需要动态构建查询。

例如价格(使用PDO):

$vars = array();
$query = "SELECT * FROM Product WHERE 1";    // using WHERE 1 just for the example
...
if (!empty($_POST["ProductPrice"]))
{
  $query .= " AND ProductPrice <= :ProductPrice";
  $vars[':ProductPrice'] = $_POST["ProductPrice"];
}
...
// prepare query
// execute query with the $vars array

条件取决于你,我只是把价格作为这个例子的最大值。