gpt4 book ai didi

php - PDO 分页在显示第一个结果后不显示数据

转载 作者:行者123 更新时间:2023-11-28 23:13:37 26 4
gpt4 key购买 nike

我正在尝试创建一个分页页面,该页面每页仅显示搜索查询的一个结果,但在显示第一个结果后,下一个结果或页面将给我一个没有输入有效输入的错误 First Result Shows right stuff Other result(2) showing this error

 <?php
define("ROW_PER_PAGE",1);
$search = '';
if (!empty($_POST['search'])) {
$search = $_POST['search'];

/* Search Entry */

$keywords = explode(' ', $search);
$arr_length = count($keywords);
$sub_query = '';

for ($i = 0; $i < $arr_length; $i++) {
if ($i == 0) {
$sub_query = $sub_query . 'sym_body LIKE "%' . $keywords[$i] . '%" OR sym_tags LIKE "%' . $keywords[$i] . '%"';
}else {
$sub_query = $sub_query . ' OR sym_body LIKE "%' . $keywords[$i] . '%" OR sym_tags LIKE "%' . $keywords[$i] . '%"';
}
}

/* Query */
$query = 'SELECT * FROM symptoms WHERE ' . $sub_query;


/*Pagination Code Starts */
$per_page_htm = '';
$page = 1;
$start = 0;
if(!empty($_GET['r'])) {
$page = $_GET['r'];
$start = ($page - 1) * ROW_PER_PAGE;
}
$limit = "LIMIT " . $start . "," . ROW_PER_PAGE;

/* Getting Row Count Tested Working */
$p_query = 'SELECT COUNT(*) FROM symptoms WHERE ' . $sub_query;
$pag_stnt = $con->query($p_query);
$row_count = $pag_stnt->fetchColumn();

echo "<h4>You have $row_count result(s)</h4>";

if (!empty($row_count)) {
$per_page_htm .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count = ceil($row_count / ROW_PER_PAGE);
if ($page_count > 1) {
$self = $_SERVER['PHP_SELF'];
for($i=1; $i<=$page_count; $i++){
if($i==$page){
$per_page_htm .= "<a href='".$self."?r=".$i."' class='btn-page current'>".$i."</a>";
}else{
$per_page_htm .= "<a href='".$self."?r=".$i."' class='btn-page'>".$i."</a>";
}
}
}
$per_page_htm .= "</div>";




$n_query = 'SELECT * FROM symptoms WHERE ' . $sub_query . ' '.$limit ;

foreach ($con->query($n_query) as $row) {

$sym_cat_id = $row['cat_id'];
$sym_pro = $row['sym_pro'];

$sym_body = $row['sym_body'];
$sym_ans = $row['sym_answer'];



?>
<!-- Symptom Area-->

<div class="panel-group results">
<div class="panel panel-success">
<div class="panel-heading">
<p><?php //echo $cat_title;?></p>
<?php echo $search; ?>
</div>
<div class="panel-body">
<?php echo $sym_body; ?>
<p><h4>Answer</h4>
<?php echo $sym_ans;?></p>
<p><h4>Was the answer helpful</h4></p>

<p>
<a href="index.php" class="btn btn-info pull-left"> Yes</a>
<button type="button" class="btn btn-danger pull-right">No</button>
</p>
</div>
</div>
</div>
<?php
}
}else{
echo '<script>
setTimeout(function(){
swal({
title: "Sorry!",
text: "Your Query is not in our database",
type: "info"

}, function(){
window.location = "index.php";
});
}, 1000);
</script>';}
echo $per_page_htm;
}else{
echo '<script>
setTimeout(function(){
swal({
title: "Oops!",
text: "Please enter a valid value!",
type: "error"

}, function(){
window.location = "index.php";
});
}, 1000);
</script>';} ?>

最佳答案

... after displaying the first result, the next result or page will give me an error that no valid input was inputted

那是因为当您点击第 2、3、... 页时(从第 1 页导航之后),$_POST 数组将为空,即 $_POST['search' ] 不会被设置。由于您没有使用表单发送任何敏感数据,因此在表单的 method 属性中使用 GET 而不是 POST ,如下所示:

<form action="..." method="get">

并像这样获取用户输入的数据:

if (!empty($_GET['search'])) {
$search = $_GET['search'];
...

随后,您需要在每个分页链接中附加该搜索查询,以便在您从一个页面跳到另一个页面时可以使用该搜索查询。

// your code
for($i=1; $i<=$page_count; $i++){
if($i==$page){
$per_page_htm .= "<a href='".$self."?r=".$i."&search=".urlencode($search)."' class='btn-page current'>".$i."</a>";
}else{
$per_page_htm .= "<a href='".$self."?r=".$i."&search=".urlencode($search)."' class='btn-page'>".$i."</a>";
}
}
// your code

旁注:了解 prepared statement因为现在您的查询很容易受到 SQL 注入(inject)攻击。另见 how you can prevent SQL injection in PHP .

关于php - PDO 分页在显示第一个结果后不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44864808/

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