gpt4 book ai didi

php - Bindparam 不适用于我的分页脚本

转载 作者:行者123 更新时间:2023-11-29 10:17:09 33 4
gpt4 key购买 nike

我找到了this脚本,因为可能容易受到 SQL 注入(inject)的攻击,我尝试绑定(bind)参数 starting_limit 变量,但不起作用。这是我的代码:

include("include/config.php");
include("include/functions.php");

$query = $connect->prepare("SELECT * FROM tutorials");
$query->execute();
$total_results = $query->rowCount();
$total_pages = ceil($total_results / $paglimit);
if (!isset($_GET["page"])) {
$page = 1;
$_GET["page"] = 1;
} else {
$page = $_GET["page"];
}
$starting_limit = ($page - 1) * $paglimit;
?>
<!DOCTYPE html>
<html lang="en">

<body>
<div class="list-group mt-4 posts-list-custom">
<? $query = $connect->prepare("SELECT * FROM tutorials ORDER BY id DESC LIMIT :starting_limit , $paglimit"); $query->bindParam(":starting_limit", $starting_limit); $query->execute(); $result = $query->fetchAll(); foreach($result as $row) { ?>
<a href="<?=$tutorialsroot . str_replace(" ", "-", strtolower($row["title"])); ?>" class="list-group-item"><?=$row["title"];?></a>
<? } ?>
</div>
<ul class="pagination mt-3">
<? for ($page = 1; $page <= $total_pages ; $page++): ?>
<li class="page-item<? if($page == $_GET["page"]) { echo " active"; }?>"><a href="<?=$tutorialsroot . "?page=" . $page;?>" class="page-link"><?=$page;?></a></li>
<? endfor; ?>
</ul>
</body>
</html>

这是我得到的错误:

Fatal error : Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''0' , 5' at line 1' in /home/rnywimzk/public_html/tutorials_subdomain/index.php:38 Stack trace: #0 /home/rnywimzk/public_html/tutorials_subdomain/index.php(38): PDOStatement->execute() #1 {main} thrown in /home/rnywimzk/public_html/tutorials_subdomain/index.php on line 38

最佳答案

1/ 我认为这不是一个好的合成器:

$query = $connect->prepare("SELECT * FROM tutorials 
ORDER BY id DESC LIMIT :starting_limit , $paglimit");

你的意思可能是:

$query = $connect->prepare("SELECT * FROM tutorials 
ORDER BY id DESC LIMIT $paglimit OFFSET :starting_limit");

2/ 您必须指定 PDOStatement::bindParam $data_type 参数,默认情况下为 PDO::PARAM_STR.

( http://php.net/manual/en/pdostatement.bindparam.php )

在您的情况下,starting_limit 的类型应为 PDO::PARAM_INT

更改:

$query->bindParam(":starting_limit", $starting_limit); 

致:

$query->bindParam(":starting_limit", $starting_limit, PDO::PARAM_INT); 

关于php - Bindparam 不适用于我的分页脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49963603/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com