php mysql 大小写语法错误

php mysql case syntax mistake

这是opencart的一部分:

model/catalog/product.php

if (!empty($data['filter_manufacturer_id'])) {
  $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
}

$sql .= " GROUP BY p.product_id";

$sort_data = array(
    'pd.name',
    'p.model',
    'p.quantity',
    'p.price',
    'rating',
    'p.sort_order',
    'p.date_added'
);

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
    if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
        $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
    } elseif ($data['sort'] == 'p.price') {
        $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
    } else {
        $sql .= " ORDER BY " . $data['sort'];
    }
} else {
    $sql .= " ORDER BY p.sort_order";
}

我正在尝试创建 "price range" 过滤器。这是我的代码:

    //Filter products based on slider price range

    if ((isset($this->request->get['lower']))&&(isset($this->request->get['higher'])))
    {
    $sql .=  " AND p.price >='". $this->request->get['lower'] ." ' AND p.price <='". $this->request->get['higher'] ."'" ;
    }

    //Filter products based on price slider

if (!empty($data['filter_manufacturer_id'])) {
    $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
}

$sql .= " GROUP BY p.product_id";

$sort_data = array(
    'pd.name',
    'p.model',
    'p.quantity',
    'p.price',
    'rating',
    'p.sort_order',
    'p.date_added'
);

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
    if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
        $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
    } elseif ($data['sort'] == 'p.price') {
        $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
    } else {
        $sql .= " ORDER BY " . $data['sort'];
    }
} else {
    $sql .= " ORDER BY p.sort_order";
}

有效!但是当我们商店的产品有特价时,它不起作用。所以我试图从 SORT BY PRICE 复制一些代码。我复制了这个 SQL:

(CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)

到我的自定义代码:

    //Filter products based on slider price range

    if ((isset($this->request->get['lower']))&&(isset($this->request->get['higher'])))
    {
    $sql .=  " AND (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) >='". $this->request->get['lower'] ." ' AND (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) <='". $this->request->get['higher'] ."'" ;
    }

    //Filter products based on price slider

但还是不行。我收到此错误:

Notice: Error: Unknown column 'special' in 'where clause' Error No: 1054

检查 "special" 列是否存在于 table 的列列表中。 或在列名前使用别名 (p.special)。

检查两个选项。

以下是我将采取的故障排除步骤:

  1. 确保 special 包含在您的 SELECT 子句中
  2. 确保您不需要使用 table 前缀,例如 p
  3. 进行 SQL 比较时可能不应引用数字

CASE 表达式的格式似乎正确,所以我认为表达式没有问题。

重要提示:我强烈建议您永远不要在生产环境中使用上面的代码。通过采用 unfiltered/validated/escaped GET 参数并将它们添加到查询字符串,您对 SQL 注入敞开大门。