POST中的php变量自动递增


auto-increment php variable within POST

我很难准确理解这段代码出了什么问题。如果有人能帮我解释我做错了什么,我会很高兴的。在底部,有一张带有"下一步"按钮的桌子。每次有人点击这个按钮,我都想增加两个值。第一个$nextLimit很好——每次按"下一步"它都会增加10,但第二个$pageNumber不起作用。我希望每次点击"下一步"时它都增加1,但它一直保持在0,我不知道为什么。

<?php
if (isset($_POST['next'])) {
    $postedLimit = (isset($_POST['next']) ? (int) $_POST['next'] : 0);
    $nextLimit = $postedLimit + 10;
// Begin code that isn't working....
    session_start();
      if (isset($_SESSION['page'])) { /* If there is already a value set */
      $_SESSION['page']++; /* Increment by 1 */
      }
      else { /* If there is no value set, ie the user is clicking the button for the first time */
        $_SESSION['page'] = 1; /* Set to 1 */
      }
    $_SESSION['page'] =  $pageNumber;
// End code that isn't working....
    echo $pageNumber ; 
    echo "<br>" ; 
    echo $nextLimit ;

    $result = mysqli_query($con, "SELECT * FROM report WHERE PMName = '$PMSelection' AND REGNSB <> 0.000 ORDER BY RegNSB DESC Limit $nextLimit,$LimitItems");
}
?>

然后,链接回$_POST['next']的HTML

<table id="box-table-a" stlye="widgth:20%">
<form method="POST" action="">
<div class="container">
<div class="right">Page <?= $pageNumber ?> of <?= $totalPages ?> 
<input type="submit" name="previous" value="prev">  
<input type="submit" name="next" value="next" onclick="this.value=<?php echo $nextLimit; ?>">
</div>
</form>
</div>
</table>

我假设当我按下"下一步"时,代码会转到

if (isset($_POST['next']))

行,然后在那里执行代码。会话应该初始化,$pageNumber每次都应该增加1,但事实并非如此。我做错了什么?

替换此代码:

if (isset($_SESSION['page'])) { /* If there is already a value set */
    $_SESSION['page']++; /* Increment by 1 */
}
else { /* If there is no value set, ie the user is clicking the button for the first time */
    $_SESSION['page'] = 1; /* Set to 1 */
}
$_SESSION['page'] =  $pageNumber;

带有

$_SESSION['page'] = (int) $nextLimit / 10;