php select语句依赖于现有变量

发布于 2024-11-13 08:05:58 字数 1066 浏览 2 评论 0原文

我正在尝试为我正在开发的一家小商店建立一个过滤系统。基本上,我正在根据变量列表计算结果。页面 products.php 如果没有查询字符串将显示所有产品。但是,如果存在变量,我希望它在必要时更改 select 语句。

但是,我在过滤时遇到问题。如果我对所有变量使用 AND 语句,它就不会正确过滤,OR 也不会。因此,如果我想要黑色 5 号鞋,无论颜色如何,它都会显示所有 5 号鞋。

有没有更好的方法来解决这个问题,因为它已经成为一个相当老的令人头疼的人了!

if ($queryString == NULL){
    $query = "SELECT * FROM store_products 
JOIN store_cat on store_products.cat=store_cat.cat_id 
JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
JOIN prod_color on store_products.prod_id=prod_color.product_id 
GROUP BY store_products.prod_id";   
}else{
        $query = "SELECT * FROM store_products 
JOIN store_cat on store_products.cat=store_cat.cat_id 
JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
JOIN prod_color on store_products.prod_id=prod_color.product_id 
where store_products.sale='$sale' OR prod_color.color='$color' OR prod_sizes.size='$size' OR store_products.cat='$cat' GROUP BY store_products.prod_id";
}

示例变量字符串

products.php?sale=&cat_id=1&size=&color=Yellow

I am trying to set up a filter system for a small shop I am developing. Basically, I am working the results off a list of variables. the page, products.php if there is no querystring will show all products. However, if there is a variable present I want it to alter the select statement where necessary.

however, I am having problems filtering. If i use the AND statement for all variables it does not filter appropriately neither does the OR. So if i wanted black size 5 shoes it would show all size fives regardless of color.

Is there a better approach to take with this as is becoming a fair old head scratcher!

if ($queryString == NULL){
    $query = "SELECT * FROM store_products 
JOIN store_cat on store_products.cat=store_cat.cat_id 
JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
JOIN prod_color on store_products.prod_id=prod_color.product_id 
GROUP BY store_products.prod_id";   
}else{
        $query = "SELECT * FROM store_products 
JOIN store_cat on store_products.cat=store_cat.cat_id 
JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
JOIN prod_color on store_products.prod_id=prod_color.product_id 
where store_products.sale='$sale' OR prod_color.color='$color' OR prod_sizes.size='$size' OR store_products.cat='$cat' GROUP BY store_products.prod_id";
}

example variable string

products.php?sale=&cat_id=1&size=&color=Yellow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅冇旳回忆 2024-11-20 08:05:58

在运行查询之前尝试构建 WHERE 标准。

字符串:
products.php?sale=&cat_id=1&size=&color=Yellow

myfile.php

$criteria = "WHERE 1 ";

if (isset($_GET['sale']) {
    $criteria .= "AND tore_products.sale=$_GET['sale'] ";
}
if (isset($_GET['cat_id']) {
    $criteria .= "AND store_products.cat=$_GET['cat_id'] ";
}
if (isset($_GET['color']) {
    $criteria .= "AND tore_products.color=$_GET['color'] ";
}

等等然后运行您的查询:

$query = "SELECT * FROM store_products 
JOIN store_cat on store_products.cat=store_cat.cat_id 
JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
JOIN prod_color on store_products.prod_id=prod_color.product_id
$criteria
GROUP BY store_products.prod_id";

并且请记住先清理您的 $_GET 变量使用mysql_real_escape_stringmysqliPDO来查询它们

Try building your WHERE criteria before running the query.

String:
products.php?sale=&cat_id=1&size=&color=Yellow

myfile.php

$criteria = "WHERE 1 ";

if (isset($_GET['sale']) {
    $criteria .= "AND tore_products.sale=$_GET['sale'] ";
}
if (isset($_GET['cat_id']) {
    $criteria .= "AND store_products.cat=$_GET['cat_id'] ";
}
if (isset($_GET['color']) {
    $criteria .= "AND tore_products.color=$_GET['color'] ";
}

And so on then run your query:

$query = "SELECT * FROM store_products 
JOIN store_cat on store_products.cat=store_cat.cat_id 
JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
JOIN prod_color on store_products.prod_id=prod_color.product_id
$criteria
GROUP BY store_products.prod_id";

And please remember to clean your $_GET variables prior to querying with them using mysql_real_escape_string, mysqli or PDO!

深府石板幽径 2024-11-20 08:05:58
$query = "SELECT * FROM store_products 
    JOIN store_cat on store_products.cat=store_cat.cat_id 
    JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
    JOIN prod_color on store_products.prod_id=prod_color.product_id ";

if {$queryString != NULL){
$query .= " WHERE tore_products.sale='$sale' OR prod_color.color='$color' OR prod_sizes.size='$size' OR store_products.cat='$cat'"
}
$query .=" GROUP BY store_products.prod_id;"

然后你执行你的查询:)

$query = "SELECT * FROM store_products 
    JOIN store_cat on store_products.cat=store_cat.cat_id 
    JOIN prod_sizes on store_products.prod_id=prod_sizes.product_id
    JOIN prod_color on store_products.prod_id=prod_color.product_id ";

if {$queryString != NULL){
$query .= " WHERE tore_products.sale='$sale' OR prod_color.color='$color' OR prod_sizes.size='$size' OR store_products.cat='$cat'"
}
$query .=" GROUP BY store_products.prod_id;"

Then you execute your Query :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文