单击按钮后$ _SESSION变量丢失

时间:2013-05-14 09:19:20

标签: php html mysql

我知道之前已经问过这个问题,但我似乎无法做到这一点......我有一个带有几个文本框和一个按钮的PHP表单。

该按钮将调用不同的函数,具体取决于早期声明的一个变量的值。我意识到变量在“继续”按钮单击后丢失了,所以我认为将它分配给$_SESSION变量可能有所帮助。不是!

根据要求,这是此页面的完整代码......就像目前一样。请注意,我已经尝试了已经回复的所有建议,即使它现在没有包含在版本中!! ..

<?php 
require_once  ("Includes/connectDB.php");
session_start();
$pn = $_GET['q'];
$hideval = $pn;  //values are as expected here...(the calling form has checks in place to ensure valid number entered

$query = "SELECT * FROM customer WHERE Phone='$pn'";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

$rowinfo = mysql_fetch_array($result);

$cphone = $pn;
if ($rows > 0)
{
    $clast = $rowinfo['LastName'];
    $cfirst = $rowinfo['FirstName'];
    $caddr1 = $rowinfo['Address1'];
    $caddr2 = $rowinfo['Address2'];
    $cpostal = $rowinfo['Postal'];
    $ccity = $rowinfo['City'];
    $cprov = $rowinfo['Province'];
    $cpay = $rowinfo['PayType'];
    $corder = $rowinfo['CustOrder'];
}
else
{
    $clast = '';
    $cfirst = '';
    $caddr1 = '';
    $caddr2 = '';
    $cpostal = '';
    $ccity = '';
    $cprov = 'ON';  
    $cpay = ''; 
    $corder = '';  
}

    function AddRecord()
{
    $clast = $_POST['CustLastName'];
    $cfirst = $_POST['CustFirstName'];
    $cphone = $_POST['CustPhone'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if(! get_magic_quotes_gpc() )
    {
        $cphone = addslashes ($cphone);
        $clast = addslashes ($_POST['CustLastName']);
        $cfirst = addslashes ($_POST['CustFirstName']);
        $caddr1 = addslashes ($_POST['CustAddress1']);
        $caddr2 = addslashes ($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addcslashes($_POST['CustOrder']);
    }
    else
    {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']); 
        $corder = $_POST['CustOrder'];
    }

    $query = "INSERT INTO customer (Phone, LastName, FirstName, Address1, Address2, City, Province, Postal, PayType, CustOrder) VALUES ('$cphone','$clast','$cfirst', '$caddr1', '$caddr2', '$City', '$Province', '$Postal', '$cpay', '$corder')";
    $result = mysql_query($query);
    if (!$result) die ("Database access failed: " . mysql_error());
}

function EditRecord()
{
    echo ('Im going to try an edit'); die;
    $clast = $_POST['CustLastName'];
    $cphone = $_POST['CustPhone'];
    $cfirst = $_POST['CustFirstName'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if(! get_magic_quotes_gpc())
    {
        $clast = addslashes ($_POST['CustLastName']);
        $cfirst = addslashes ($_POST['CustFirstName']);
        $caddr1 = addslashes ($_POST['CustAddress1']);
        $caddr2 = addslashes ($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addslashes($_POST['CustOrder']);
     }
     else
     {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']);
        $corder = $_POST['CustOrder'];
     }

     $query = "UPDATE customer SET Phone='$cphone', LastName='$clast', FirstName='$cfirst', Address1='$caddr1', Address2='$caddr2', City='$ccity', Province='$cprov', Postal='$cpostal', PayType='$cpay', CustOrder='$corder' WHERE Phone='$cphone'";
     $result = mysql_query($query);

     if (!$result) die ("Database access failed: " . mysql_error());
 }


?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="styles/beer_delivery.css" />

</head>

<body>
<p></p>
<br><br>
<div id="mainPicture">
    <div class="picture">

    </div>
</div>
    <div class="contentBox">
    <div class="innerBox">
        <h1>Customer Information</h1>
        <div class="contentText">
            <p></p>
            <div id="main">
                <form action="customer_info.php" method="post">
                    <fieldset>
                        <legend>Customer</legend>
                            <label for="CustPhone">Phone:</label>

                            <input type="text" name="CustPhone" id="CustPhone" tabindex="2" value="<?php echo $cphone;  ?>">                  
                            <br>
                            <div id="divider"></div>

                          <p>&nbsp;</p>
                           <label for="CustLastName">Last Name:</label>
                           <input type="text" name="CustLastName" id="CustLastName" tabindex="2" value="<?php echo $clast;  ?>">
                           &nbsp;&nbsp;&nbsp;&nbsp;
                           <label for="CustFirstName">First Name:</label>
                           <input type="text" name="CustFirstName" id="CustFirstName" tabindex="3" value="<?php echo $cfirst;  ?>">

                           <p>&nbsp;</p>

                           <label for="CustAddress1">Address 1:</label>
                           <input type="text" name="CustAddress1" id="CustAddress1" tabindex="4" value="<?php echo $caddr1; ?>">

                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <label for="CustAddress2">Address2:</label>
                            <input type="text" name="CustAddress2" id="CustAddress2" tabindex="5" value="<?php echo $caddr2; ?>">

                            <p>&nbsp;</p>

                            <label for="CustCity">City:</label>
                            <input type="text" name="CustCity" id="CustCity" tabindex="6" value="<?php echo $ccity; ?>">

                            <p>&nbsp;</p>

                            <label for="CustPostal">PostalCode</label>
                            <input type="text" name="CustPostal" id="CustPostal" tabindex="7" value="<?php echo $cpostal; ?>">

                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <label for="CustProvince">Province:</label>
                            <input type="text" name="CustProvince" id="CustProvince" tabindex="8" value="<?php echo $cprov; ?>">

                            <p>&nbsp;</p>
                            <label for="CustOrder">Customer Order:</label>
                            <textarea name="CustOrder" rows="4" cols="50" tabindex="9"><?php echo $corder; ?></textarea>
                            <p>&nbsp;</p>

                            <label for="PayType">Method of Payment:</label>
                            <select name="PayType" id="PayType" tabindex="10">
                                <option value="Credit Card" <?php if ($cpay == 'Credit Card') echo ' selected="selected"';?>>Credit Card</option>
                                <option value="Debit" <?php if ($cpay == 'Debit') echo ' selected="selected"';?>>Debit</option>
                                <option value="Cheque" <?php if ($cpay == 'Cheque') echo ' selected="selected"';?>>Cheque</option>
                                <option value="Other"<?php if ($cpay == 'Other') echo ' selected="selected"'; ?>>Other</option>
                            </select> 

                            <p>&nbsp;</p>
                            &nbsp;&nbsp;&nbsp;&nbsp;

                           <form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF']."?".http_build_query($_GET); ?>">
                           <input type="submit" name="Continue" id="Continue" value="Continue" tabindex="11">

                            </form> 
                            <?php
                               //have also used session vars here...but same problem
                                echo $_GET['q']; //this shows the phone number
                                  if(isset($_POST['Continue']))
                                  {
                                      //ultimately, the functions AddRecord and EditRecord will be called in here ... once I get the variable problem fixed
                                      echo 'after click: '. $_GET['q'];  //this shows "after click: "
                                  }
                            ?>                    

                            <p>&nbsp;</p>
                       </fieldset>
                  </form>
            </div>   
        </div>
    </div>
</div>

</body>
</html>

4 个答案:

答案 0 :(得分:2)

这就是MildlyInteresting所说的。尝试替换

$hideval = $_GET['q']; 

if(!empty($_GET['q'])) {
    $hideval = $_GET['q'];
}

答案 1 :(得分:1)

原因是您在表单的第二个网站上声明$_SESSION['hv'] = $hideval;,因为相应的表单元素不存在,因此可能具有null值。

相反,你可能会想要这样声明:

if(!empty($_GET['q'])) {
    $_SESSION['hv'] = $_GET['q'];
}

在下一个站点上,会话变量不会被null覆盖,您可以按预期访问它。

答案 2 :(得分:0)

显然,$ _POST ['Continue']是问题所在,您可以使用var_dump打印$ _POST变量,以检查表单中的数据。

  

的var_dump($ _ POST);

如果没有$ _POST ['Continue'],肯定你无法获得$ hideval的值。 希望对你有用!

答案 3 :(得分:0)

你在说

$hideval = $_GET['q'];

<form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

请注意,我们不会发送HTTP query string,因此当您点击“继续”时,系统不会设置$_GET['q']

尝试按如下方式编写:

<form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF']."?".http_build_query($_GET); ?>">

另外,我认为强烈建议将错误报告级别设置为最严格的模式,以查看所有错误(假设您没有测试您的网站'online',启用当你还在测试时看世界:

error_reporting(-1); // Display *ALL* PHP errors, enabling you to see every error PHP will emit.

编辑:

对我来说,这个正在运作:

<?php

//require_once  ("Includes/connectDB.php");
session_start();
$pn = $_GET['q'];
$hideval = $pn;  //values are as expected here...(the calling form has checks in place to ensure valid number entered

$query = "SELECT * FROM customer WHERE Phone='$pn'";
$result = mysql_query($query);

if (!$result)
    echo ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

$rowinfo = mysql_fetch_array($result);

$cphone = $pn;
if ($rows > 0) {
    $clast = $rowinfo['LastName'];
    $cfirst = $rowinfo['FirstName'];
    $caddr1 = $rowinfo['Address1'];
    $caddr2 = $rowinfo['Address2'];
    $cpostal = $rowinfo['Postal'];
    $ccity = $rowinfo['City'];
    $cprov = $rowinfo['Province'];
    $cpay = $rowinfo['PayType'];
    $corder = $rowinfo['CustOrder'];
}
else {
    $clast = '';
    $cfirst = '';
    $caddr1 = '';
    $caddr2 = '';
    $cpostal = '';
    $ccity = '';
    $cprov = 'ON';
    $cpay = '';
    $corder = '';
}

function AddRecord() {
    $clast = $_POST['CustLastName'];
    $cfirst = $_POST['CustFirstName'];
    $cphone = $_POST['CustPhone'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if (!get_magic_quotes_gpc()) {
        $cphone = addslashes($cphone);
        $clast = addslashes($_POST['CustLastName']);
        $cfirst = addslashes($_POST['CustFirstName']);
        $caddr1 = addslashes($_POST['CustAddress1']);
        $caddr2 = addslashes($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addcslashes($_POST['CustOrder']);
    } else {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']);
        $corder = $_POST['CustOrder'];
    }

    $query = "INSERT INTO customer (Phone, LastName, FirstName, Address1, Address2, City, Province, Postal, PayType, CustOrder) VALUES ('$cphone','$clast','$cfirst', '$caddr1', '$caddr2', '$City', '$Province', '$Postal', '$cpay', '$corder')";
    $result = mysql_query($query);
    if (!$result)
        die("Database access failed: " . mysql_error());
}

function EditRecord() {
    echo ('Im going to try an edit');
    die;
    $clast = $_POST['CustLastName'];
    $cphone = $_POST['CustPhone'];
    $cfirst = $_POST['CustFirstName'];
    $caddr1 = $_POST['CustAddress1'];
    $caddr2 = $_POST['CustAddress2'];
    $ccity = $_POST['CustCity'];
    $cpostal = $_POST['CustPostal'];
    $cpay = $_POST['PayType'];
    $corder = $_POST['CustOrder'];

    if (!get_magic_quotes_gpc()) {
        $clast = addslashes($_POST['CustLastName']);
        $cfirst = addslashes($_POST['CustFirstName']);
        $caddr1 = addslashes($_POST['CustAddress1']);
        $caddr2 = addslashes($_POST['CustAddress2']);
        $ccity = addcslashes($_POST['CustCity']);
        $cpostal = addcslashes($_POST['CustPostal']);
        $corder = addslashes($_POST['CustOrder']);
    } else {
        $clast = ($_POST['CustLastName']);
        $cfirst = ($_POST['CustFirstName']);
        $caddr1 = ($_POST['CustAddress1']);
        $caddr2 = ($_POST['CustAddress2']);
        $ccity = ($_POST['CustCity']);
        $cpostal = ($_POST['CustPostal']);
        $corder = $_POST['CustOrder'];
    }

    $query = "UPDATE customer SET Phone='$cphone', LastName='$clast', FirstName='$cfirst', Address1='$caddr1', Address2='$caddr2', City='$ccity', Province='$cprov', Postal='$cpostal', PayType='$cpay', CustOrder='$corder' WHERE Phone='$cphone'";
    $result = mysql_query($query);

    if (!$result)
        echo ("Database access failed: " . mysql_error());
}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="styles/beer_delivery.css" />
    </head>
    <body>
        <p></p>
        <br><br>
                <div id="mainPicture">
                    <div class="picture">

                    </div>
                </div>
                <div class="contentBox">
                    <div class="innerBox">
                        <h1>Customer Information</h1>
                        <div class="contentText">
                            <p></p>
                            <div id="main">
                                <form action="<?php echo $_SERVER['PHP_SELF'] . "?" . http_build_query($_GET); ?>" method="post">
                                    <fieldset>
                                        <legend>Customer</legend>
                                        <label for="CustPhone">Phone:</label>

                                        <input type="text" name="CustPhone" id="CustPhone" tabindex="2" value="<?php echo $cphone; ?>">
                                            <br>
                                                <div id="divider"></div>

                                                <p>&nbsp;</p>
                                                <label for="CustLastName">Last Name:</label>
                                                <input type="text" name="CustLastName" id="CustLastName" tabindex="2" value="<?php echo $clast; ?>">
                                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                                    <label for="CustFirstName">First Name:</label>
                                                    <input type="text" name="CustFirstName" id="CustFirstName" tabindex="3" value="<?php echo $cfirst; ?>">

                                                        <p>&nbsp;</p>

                                                        <label for="CustAddress1">Address 1:</label>
                                                        <input type="text" name="CustAddress1" id="CustAddress1" tabindex="4" value="<?php echo $caddr1; ?>">

                                                            &nbsp;&nbsp;&nbsp;&nbsp;
                                                            <label for="CustAddress2">Address2:</label>
                                                            <input type="text" name="CustAddress2" id="CustAddress2" tabindex="5" value="<?php echo $caddr2; ?>">

                                                                <p>&nbsp;</p>

                                                                <label for="CustCity">City:</label>
                                                                <input type="text" name="CustCity" id="CustCity" tabindex="6" value="<?php echo $ccity; ?>">

                                                                    <p>&nbsp;</p>

                                                                    <label for="CustPostal">PostalCode</label>
                                                                    <input type="text" name="CustPostal" id="CustPostal" tabindex="7" value="<?php echo $cpostal; ?>">

                                                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                                                        <label for="CustProvince">Province:</label>
                                                                        <input type="text" name="CustProvince" id="CustProvince" tabindex="8" value="<?php echo $cprov; ?>">

                                                                            <p>&nbsp;</p>
                                                                            <label for="CustOrder">Customer Order:</label>
                                                                            <textarea name="CustOrder" rows="4" cols="50" tabindex="9"><?php echo $corder; ?></textarea>
                                                                            <p>&nbsp;</p>

                                                                            <label for="PayType">Method of Payment:</label>
                                                                            <select name="PayType" id="PayType" tabindex="10">
                                                                                <option value="Credit Card" <?php if ($cpay == 'Credit Card') echo ' selected="selected"'; ?>>Credit Card</option>
                                                                                <option value="Debit" <?php if ($cpay == 'Debit') echo ' selected="selected"'; ?>>Debit</option>
                                                                                <option value="Cheque" <?php if ($cpay == 'Cheque') echo ' selected="selected"'; ?>>Cheque</option>
                                                                                <option value="Other"<?php if ($cpay == 'Other') echo ' selected="selected"'; ?>>Other</option>
                                                                            </select>

                                                                            <p>&nbsp;</p>
                                                                            &nbsp;&nbsp;&nbsp;&nbsp;

                                                                            <form id="custform" method="post" action="<?php echo $_SERVER['PHP_SELF'] . "?" . http_build_query($_GET); ?>">
                                                                                <input type="submit" name="Continue" id="Continue" value="Continue" tabindex="11">

                                                                            </form>
<?php

//have also used session vars here...but same problem
echo $_GET['q']; //this shows the phone number
if (isset($_POST['Continue'])) {
    //ultimately, the functions AddRecord and EditRecord will be called in here ... once I get the variable problem fixed
    echo 'after click: ' . $_GET['q'];  //this shows "after click: "
}

?>

                                <p>&nbsp;</p>
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </body>
</html>

当我键入http://example.com/customer_info.php?q=4 example.com 作为您正在测试它的服务器(通常是 localhost ),进入地址栏时,该网站会在继续按钮后显示:

  

4

当我点击按钮时,它会显示:

  

4点击后:4

对我而言,它正在发挥作用。如果它仍然不起作用,请检查会话设置。也许有一些(错误的)它。