Php价格asc功能不起作用

时间:2017-12-05 18:27:00

标签: php mysql

我正在尝试在php中进行价格上升或降序过滤,但它无法正常工作。有人可以帮忙修复它吗?谢谢

<select name='sort'>
<option value='ASC'> Price Low to High </option>
<option value='DESC'> Price High to Low </option>

</select>
<?php 
$query = 'SELECT prd_price FROM products ORDER BY '.$_REQUEST['sort'];
$run_query = mysqli_query($con,$query);

$row = mysqli_fetch_array($run_query);
?>

1 个答案:

答案 0 :(得分:1)

你不能这样做。导致您的select未得到处理。

当您阅读关于$_REQUEST的php docs时,您会发现它只会处理$_POST$_GET$_COOKIE的值。

如果你想让这个工作,你可以做这样的事情:

<form method="post">
    <select name="sort">
        <option value="asc">Price Low to High</option>
        <option value="desc">Prive High to Low</option>
    </select>
    <input type="submit" value="Sort">
</form>
<?php
// check if the server recieved a post:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (isset($_POST['sort'])) {
        if ($_POST['sort'] == "asc") {
            $query = "SELECT prd_price FROM products ORDER BY prd_price ASC";
        } else {
            $query = "SELECT prd_price FROM products ORDER BY prd_price DESC";
        }
    }
    // execute query here.
}
?>

尽量避免使用$_REQUEST