我在开始时有会话变量,但在点击按钮后丢失了。这是我的代码,$ _SESSION [' InputCount']在开头不是空的,但在我点击按钮后我无法达到会话变量。 isset块中的$ iteration变量为null。此外,我有一个sesion变量,从上一页保存用户ID,它没有问题它仍然是真的。 $ iteration有什么问题?
<?php
require("includes/db.php");
require("includes/functions.php");
session_start();
$inputCount = mysql_real_escape_string($_POST['inputCount']);
$_SESSION['InputCount'] = $inputCount; //it is ok at the begining
//check if the form has been submitted
if(isset($_POST['checkoutButton'])){
$iteration = $_SESSION['InputCount'];
var_dump ($iteration);
$userID = $_SESSION['UserID'];
答案 0 :(得分:0)
无论网页的加载方式如何,$ _POST [&#39; inputCount&#39;]的值都会分配给您的会话变量。
换句话说,当您使用结帐按钮回发时,不是从会话中提取inputCount的值,而是根据当前的$ _POST [&#39; inputCount&#39; ],然后从会话中读取(这是多余的)。
你可能想要的是这样的:
require("includes/db.php");
require("includes/functions.php");
session_start();
//check if the form has been submitted
if(!isset($_POST['checkoutButton'])){
$inputCount = mysql_real_escape_string($_POST['inputCount']);
$_SESSION['InputCount'] = $inputCount; //it is ok at the begining
} else {
$iteration = $_SESSION['InputCount'];
var_dump ($iteration);
$userID = $_SESSION['UserID'];
}