php mysql搜索所有$var

k3bvogb1  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(261)

有人能看出我是如何正确编码的而不是我选择的路线吗,
基本上我希望过滤所有字段,如果选中
从选项
托收交付本地托收或全部
我可以正确地向上查询,直到我希望从数据库中提取所有内容
下面的代码将进一步解释。
我不得不做一个单独的查询,以拉所有,因为我找不到任何其他方式。
但如果有人能给我一点建议,我会非常感激的
数据库有(delivery)yes/no/yes local,但是我的查询只能提取这些选项,但是我也希望能够提取所有选项。但是我不知道该怎么写这个查询。
参见下面的代码。

if ($delivery==="collection") { $delivery="no";}
        if ($delivery==="delivery") {$delivery="yes";}
        if ($delivery==="local") {$delivery="yes local";}

        if ($delivery==="either") {$delivery="either";}

if ($delivery != "either")    
{       
$query="SELECT * FROM testdata WHERE title LIKE ? AND location LIKE ? AND postcode LIKE ? AND price >=? AND price <=? AND cond=? AND catagory LIKE ? AND delivery=? ORDER BY $order $dir";

$stat=$db->prepare($query);

$stat->execute(array("%$searchfor%","%$location%","%$postcode%","$pricefrom","$priceto","$cond","%$catagory%","$delivery"));
}           

if ($delivery==="either") { $query="SELECT * FROM testdata WHERE title LIKE ? AND location LIKE ? AND postcode LIKE ? AND price >=? AND price <=? AND cond=? AND catagory LIKE ? ORDER BY $order $dir"; 

$stat=$db->prepare($query);

$stat->execute(array("%$searchfor%","%$location%","%$postcode%","$pricefrom","$priceto","$cond","%$catagory%"));

}

我希望你能理解代码,我不知道我是否解释得很好。。

9vw9lbht

9vw9lbht1#

在以下情况下,可以使用sql:

$delivery_con = [];
if ($delivery==="collection") { $delivery_con="no";}
if ($delivery==="delivery") {$delivery_con="yes";}
if ($delivery==="local") {$delivery_con="yes local";}

if ($delivery==="either") {$delivery_con="no,yes,yes local";}

$query="SELECT * FROM testdata WHERE title LIKE ? AND location LIKE ? AND postcode LIKE ? AND price >=? AND price <=? AND cond=? AND catagory LIKE ? AND delivery IN (?) ORDER BY $order $dir";

$stat=$db->prepare($query);

$stat->execute(array("%$searchfor%","%$location%","%$postcode%","$pricefrom","$priceto","$cond","%$catagory%",$delivery_con));
8nuwlpux

8nuwlpux2#

答案一直在我眼前
(如果($delivery==“任意”){$delivery=null;})
(回答/使用like通配符(只需仔细说明如何标记数据库)

if ($catagory==="View All") {$catagory=null;}

     if ($delivery==="either") {$delivery=null;}

     if ($cond==="both") {$cond=null;}

        if ($delivery==="collection") { $delivery="no";}
        if ($delivery==="delivery") {$delivery="yes";}
        if ($delivery==="local") {$delivery="local";}

$query="SELECT * FROM testdata WHERE title LIKE ? AND location LIKE ? AND postcode LIKE ? AND price >=? AND price <=? AND cond LIKE ? AND catagory LIKE ? AND delivery LIKE ? ORDER BY $order $dir";

$stat=$db->prepare($query);

$stat->execute(array("%$searchfor%","%$location%","%$postcode%","$pricefrom","$priceto","%$cond%","%$catagory%","%$delivery%"));

相关问题