在POST中自动增加php变量

时间:2014-09-23 19:19:24

标签: php

我很难理解这段代码到底出了什么问题。如果有人可以帮我向我解释我做错了什么,我会很高兴。在底部,有一个带有“下一步”按钮的表格。每次有人点击此按钮,我想增加两个值。第一个,$ nextLimit,很好 - 每次按“Next”时它会增加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,但事实并非如此。我做错了什么?

1 个答案:

答案 0 :(得分:0)

替换此代码:

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;