我已设置默认查询
$query = "SELECT Image, ISBN, Name, Vol, Release_date, publisher, price FROM products p order by Name,vol";
echo "<a href = ???>Date</a>";
以便在点击后按日期排序可以更改查询? 谢谢!
答案 0 :(得分:0)
查询结尾处的排序/排序参数为order by Name,vol
。
因此,您可以将其作为变量,具体取决于href是否存在GET['date']
。
<?php
$sortvar = isset($_GET['date']) ? 'order by '. $_GET['date'] : 'order by Name,vol';
if (isset($_GET['ndsn']) && isset($_GET['date']) )
$sortvar = 'order by '. $_GET['date'] . ',' . $_GET['ndsn'];
$query = "SELECT Image, ISBN, Name, Vol, Release_date, publisher, price FROM products p " . $sortvar;
echo "<a href = '?date=Date'>Date</a><br>";
echo "<a href = '?date=Date&ndsn=NDSN'>Date & NDSN</a><br>";
echo $query;
在向其中添加GET参数时,您必须转义GET参数或参数化结果查询,这可能导致SQL注入。