我正在尝试在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);
?>
答案 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