会话变量在选择后继续消失

时间:2014-03-02 17:53:59

标签: php session

我有来自$ _SESSION的其他网址的值。它以{$ is:$ user}或{2:bob}所需的第二个值粘在一起。我将它们与爆炸分开并“尝试”将它们分配为$ _SESSION ['id'] = $ pieces [0];和$ _SESSION ['cust_name'] = $件[1]; 这个过程第一次完美。价值是分段的,他们去了适当的地方。 但是在我选择的提交后,我失去了$ _SESSION ['cust_name']的价值 如何在选择后保留$ _SESSION ['cust_name']的值?

<?php
session_start();
if(isset($_POST['SubmitForRedirect'])){

        //store as session variable
        $_SESSION['printdata'] = $_POST['bolredir'];
        //forward browser
        die(header("Location: add-job.php"));
}

require_once("header2.php");
//var_dump($_SESSION['id']);
//var_dump($_SESSION['cust_name']);
$cust_info = $_SESSION['id'];
$pieces = explode(":", $cust_info);
//if(isset($_SESSION['cust_name']))
$_SESSION['id'] = $pieces[0];
$_SESSION['cust_name'] = $pieces[1];
//else
//echo "Something died";
echo $pieces[0];
?><br /><?php
echo $pieces[1];

?>


<!DOCTYPE html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <!-- <link href="style.css" rel="stylesheet" type="text/css" /> -->
                <title> Contacts Database </title>
        </head>

        <body>
        <h2> Select and existing job for: <?php echo $_SESSION['cust_name']; ?> with the ID of: <?php echo $_SESSION['id']; ?>  and select print options</h2>

<?php
                // selection box submit
try{
        $conn = new PDO("mysql:host=localhost;dbname=$db", $user, $pass);
        $stmt = $conn->prepare("SELECT * FROM customer INNER JOIN orders ON orders.cust_id = customer.id WHERE id =".$_SESSION['id']);
        $stmt->execute();

        $result = $stmt->fetchAll();
?>

<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="bolredir">
<option></option>

<?php
        foreach ($result as $item){
                echo '<option value='.$item['cust_id'].'>';
                echo ($item['ship_name'] .",". $item['ship_addr'] .",". $item['total_price'].",". $item['cust_id']."<br />\n");
                echo '</option>';
                }
        }
        catch (PDOException $e){
                echo 'ERROR: ' . $e->getMessage();
        }
?>
</select>
        <input type="submit" name="SubmitForRedirect" value="Submit" />
</form>
</body>

2 个答案:

答案 0 :(得分:0)

如果您希望使用会话,必须session_start()放在每个页面顶部 之前:

<?php
session_start();
require_once("header2.php");

答案 1 :(得分:0)

看起来你在每个请求上覆盖$_SESSION['id']

您通常只会在登录时设置一次。