我目前正在开发一个事件日历,允许用户在日期和地点以及日期和地点之间进行排序。
这是我用于订单的代码:
$order = isset($_GET['sort'])?$_GET['sort']:'date_added';
$result = mysqli_query($con,"SELECT * FROM calendar ORDER BY $order");
$sql = mysql_query($query);
现在,当您单击一个按钮时,它会像这样设置将用于排序的变量
onclick="location.href=' index.php?sort=date&place'"
现在这很好用,但是当我开始我的活动日历时,它是空白的......如何为我的日历提供默认订单?
以下是日历输出的代码:
echo "<table class='table table-striped' >
<thead>
<tr>
<th>Artist</th>
<th>Place</th>
<th>Date</th>
<th>Genre</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
$date = $row['date'];
$convdate = date('d-m-Y', strtotime($row['date']));
echo "<tr>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['place'] . "</td>";
echo "<td>" . $convdate . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
像这样设置顺序是正确的。但有mysql_query是没有意义的。就把它放出来。出于安全原因(SQL注入),我建议您在查询中使用之前转义字符串。
$order = isset($_GET['sort']) ? $_GET['sort'] : 'date_added';
$order = mysqli_real_escape_string($con, $order);
$result = mysqli_query($con,"SELECT * FROM calendar ORDER BY $order");
字段的名称是"日期和地点"吗?
请注意,URL 中的 & 符号应该被转义,因为如果不是,$_GET 将作为另一个不同的参数"place"。
如何为我的日历提供默认订单?
"默认订单"是什么意思?
$order = isset($_GET['sort'])?$_GET['sort']:'date_added'; $result = mysqli_query($con,"从日历顺序中选择 * $order");$sql = mysql_query($query);
切勿将未经测试的值传递给 sql 语句。
onclick="location.href=' index.php?sort=date&place'"
这不会导致 $_GET['sort']=='date&place',url 中的 & 标记一个新变量。