首页 > 解决方案 > 如何使用 PHP PDO 在同一页面上发送可变数据?

问题描述

我正在为大约 1500 行的表格进行分页。

$numperpage = 50;
$countsql = $connect->prepare("select COUNT(id) from prana");
$countsql->execute();
$row = $countsql->fetch();
$numrecords = $row[0];
$numlinks = ceil($numrecords/$numperpage); 

$page = $_GET['start'];
if (!$page) $page = 0;
$start = $page * $numperpage;

echo "start is ".$start.'<br>';

if(isset($_POST["action"]))
{
 $query = "
  SELECT * FROM prana WHERE product_status = '1' limit $start,$numperpage";

echo $output;
 
 for ($i=0;$i<$numlinks;$i++)
 {
   $y = $i+1;
   echo '<a href="index.php?start='.$i.'"> '.$y.' </a>';
 }

从 <a href="index.php?start='.$i.'"> 我在我的 url 中获取 start 的值,但对于 $page = $_GET['start'] 它显示 Undefined array key start 。并且开始变量保持在0。

这是我的参考:- https://www.youtube.com/watch?v=TI78ax23qZg 请帮忙。

标签: php

解决方案


要摆脱警告,请从以下位置更改您的代码:

$page = $_GET['start'];
if (!$page) $page = 0;

进入:

$page = isset($_REQUEST['start']) ? $_REQUEST['start'] + 0 : 0;

使用Where$_REQUEST['start']代替$_GET['start']以确保逻辑有效,即使方法不是GET

而“+ 0”是为了确保它是数字的。


此外,start在单击其中一个链接之前不会设置参数。


推荐阅读