【问题标题】:Order table by default默认订单表
【发布时间】:2016-11-29 06:46:36
【问题描述】:

我目前正在开发一个活动日历,它允许用户在日期和地点和日期和地点之间进行排序。

这是我用于订单的代码:

$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);



?>

【问题讨论】:

  • 你为什么混合mysqli_*和mysql_*? $query呢?

标签: php mysqli html-table


【解决方案1】:

这样的设置顺序是正确的。但是有 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");

【讨论】:

    【解决方案2】:

    字段名称是“日期和地点”吗?

    注意 URL 中的 & 符号应该被转义,因为如果不是,$_GET 将作为另一个不同的参数“放置”。

    【讨论】:

    • 这是两个单独的字段,日期 |地点....所以我要做的是按日期&地点和地点&日期排序
    • 你不能在不转义的情况下在 URL 中传递 & 符号:你的 PHP 脚本将在 & 符号之后像一个新变量一样接受任何东西。
    【解决方案3】:

    如何为我的日历设置默认顺序?

    “默认顺序”是什么意思?

    $order = isset($_GET['sort'])?$_GET['sort']:'date_added'; $结果 = mysqli_query($con,"SELECT * FROM calendar ORDER BY $order"); $sql = mysql_query($query);

    永远不要将未经测试的值传递给 sql 语句。

    onclick="location.href='index.php?sort=date&place'"

    这不会导致 $_GET['sort']=='date&place',url 中的 & 标记了一个新变量。

    【讨论】:

    • 我的意思是,当我打开网站时,它会自动按日期和地点对表格进行排序。
    • 如果 $_GET['sort'] 为空,只需根据您的需要设置 $order。
    猜你喜欢
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2020-02-01
    • 1970-01-01
    相关资源
    最近更新 更多