数量根本没有增加1

时间:2012-09-16 12:22:12

标签: php

如果运行提交代码,则应执行以下代码:

session_start();

if(isset($_POST['sessionNum'])) {
            //Declare my counter for the first time

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
            $_SESSION['sessionCount'] = 1;
 }
 else if (isset($_POST['submitDetails']) && 
       $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    $_SESSION['sessionCount']++;
 }

$sessionMinus =  $_SESSION['sessionCount'];

当我回复下面的代码时:

<?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?>

每次点击提交按钮时,如果$sessionMinus的数字小于数字$_SESSION['initial_count'],那么每次页面被$sessionMinus的数字减1提交。

但它并没有将数字加1。 $sessionMinus每次提交页面时都会保持输出“1”。我需要在代码中更改什么?

我执行了var_dump($_SESSION);,这就是它输出的内容:

array(3) { 
   ["sessionCount"]=> int(1) 
   ["initial_count"]=> string(1) "5" 
   ["sessionNum"]=> int(5) 
}

2 个答案:

答案 0 :(得分:0)

阅读完评论后,我认为这就是你的意思:

if(!isset($_SESSION['sessionCount']) || !$_SESSION['sessionCount'] ) {
    //Declare my counter for the first time

    $_SESSION['initial_count'] = $_POST['sessionNum'];
    $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
    $_SESSION['sessionCount'] = 1;
 }
 else if (isset($_POST['submitDetails']) && 
       $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    $_SESSION['sessionCount']++;
 }

$sessionMinus =  $_SESSION['sessionCount'];

答案 1 :(得分:0)

按如下方式调试:

session_start();

var_dump( $_POST, $_SESSION ); //See that the post and session parameters you think should be there, really are there.
if(isset($_POST['sessionNum'])) {
            //Declare my counter for the first time

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
            $_SESSION['sessionCount'] = 1;
 }
 else if (isset($_POST['submitDetails']) && 
       $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    var_dump( $_SESSION['sessionCount'] ); //check the current count (and that you really are here.
    $_SESSION['sessionCount']++;
    var_dump( $_SESSION['sessionCount'] ); // Check that the count is updated correctly.
 }

$sessionMinus =  $_SESSION['sessionCount'];