-1
排序是完美的,我把它與分頁結合起來。但現在我面臨的問題是什麼時候要將排序的值傳遞給分頁:排序不能與分頁工作
您的SQL語法錯誤; 「?頁= 1個ASC LIMIT 0,10」檢查對應於您MariaDB的服務器版本正確的語法使用附近的3號線
以下手冊是我的代碼:
<?php
session_start();
$host = 'localhost';
$user = 'root';
$pass ='password';
$name = 'dbname';
$con = new mysqli ($host,$user,$pass,$name);
?>
<html>
<h2>Your Redeemed Ticket (<?php echo $_SESSION['username'];?>):</h2>
<table border='1' align='center' cellpadding='10'>
<tr align = "center">
<td>
<a href="user_history.php?sort=purchase_id">Purchase ID</a>
<td>
<a href="user_history.php?sort=ticket_type">Ticket Type</a>
<td>
<a href="user_history.php?sort=qty">Quantity</a>
<td>
<a href="user_history.php?sort=date">Date</a>
</tr>
</html>
<?php
// default sorting
if(isset($_GET['sort'])){
$sortOrder = $_GET['sort'] ? :0;
/*Pagination*/
$limit = 10;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $limit;
$username = $_SESSION['username'];
$data =
"
SELECT *
FROM redeem
WHERE redeem_by = '$username' ORDER BY ".$sortOrder." ASC LIMIT $start_from, $limit
";
$result = $con->query($data);
if ($result === false)
die (mysqli_error($con));
while($rows = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>";
echo $rows['purchase_id'];
echo "<td align='center'>";
echo $rows['ticket_type'];
echo "<td align='center'>";
echo $rows['qty'];
echo "<td align='center'>";
echo $rows['date'];
echo "</tr>";
}
echo "</table>";
/*PAGE NUMBER DISPLAY*/
$data2 = "SELECT COUNT(redeem_by) FROM redeem";
$rs_result = mysqli_query($con,$data2);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records/$limit);
$pagLink = '';
for ($i=1; $i<=$total_pages; $i++)
{
$pagLink .="<a href='user_history.php?sort=".$sortOrder."?page=$i'>" .$i."/</a>";
};
echo "<center>".$pagLink."</center>";
/*LOGOUT*/
if(isset($_POST['logout']))
{
session_destroy();
header ("location:login.php");
exit();
}
}
?>
'頁= $ i'必須是'&頁= $ i'。你只能在URL中有一個'?'。 – deceze
看起來你的排序順序很容易受到SQL注入的影響。 如果我想按'排序;展示桌子; '例如。 考慮使用'?'準備的查詢並通過用戶輸入來傳遞更安全的查詢 – EnabrenTane
在繼續之前,請保護自己和用戶免受sql注入=> https://stackoverflow.com/questions/60174/how- can-i-prevent-sql -injection-in-php –