我网站上多个网页之间的会话不会持续存在。这是我在每个页面开头的会话代码:
<?php
@session_start();
echo session_id();
?>
然后我将变量添加到会话中:
$_SESSION["CustomerID"]= $IDCustomer;
$_SESSION["PaymentID"]= $IDPayment;
之后var转储直接显示正确的变量。但是,当在下一页上执行完全相同的操作时,变量为NULL。我在第一页上转储变量和在第二页上转储var之间唯一的办法就是点击超链接来切换页面。
这是每页的完整代码:
<?php
require_once("php/init.php");
echo session_id();
$Cat = $_GET['Type'];
//create an instance of the ADO connection object
$conn = new COM ("ADODB.Connection")
or die("Cannot start ADO");
//show the connection string
$connStr = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source= \www\DMG Games Website\DMG Database.accdb";
$conn->open($connStr); //Open the connection to the database
//Read customer + payment from $Get
//Store in session variables
$IDCustomer = $_GET['CustomersDropdown'];
$IDPayment = $_GET['PaymentsDropdown'];
$_SESSION["CustomerID"]= $IDCustomer;
$_SESSION["PaymentID"]= $IDPayment;
//create the Products query
$query = "SELECT * FROM [Product Details]";
if (isset($Cat)) {
$query = "SELECT * FROM [Product Details] Where [Product Type] ='" . $Cat . "'";
}
//execute query
$rs = $conn->execute($query);
//count the number of columns
$num_columns = $rs->Fields->Count();
echo "There are " . $num_columns . " columns." . "<br>";
for ($i=0; $i < $num_columns; $i++) {
$fld[$i] = $rs->Fields($i);
}
//show the information in a table
echo "<table>";
echo "<tr>";
echo "<th> Product ID </th>";
echo "<th> Product Name </th>";
echo "<th> Product Description </th>";
echo "<th> Price </th>";
echo "<th> Quantity In Stock </th>";
echo "<th> Product Type </th>";
echo "<th> Image </th>";
echo "<th> Click to Buy </th>";
echo "</tr>";
while (!$rs->EOF) //carry on looping while there are records to be obtained
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $fld[$i]->value . "</td>";
}
echo "</tr>";
$rs->MoveNext(); //move on to the next record
}
echo "</table>";
//close the connection
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
$Customer = $_SESSION["CustomerID"];
var_dump($Customer);
$Payment = $_SESSION["PaymentID"];
var_dump($Payment)
?>
这是第二页的代码:
//create an instance of the ADO connection object
$conn = new COM ("ADODB.Connection")
or die("Cannot start ADO");
//show the connection string
$connStr = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source= \www\DMG Games Website\DMG Database.accdb";
$conn->open($connStr); //Open the connection to the database
// Get the url variables
$Pid = $_GET['newPid'];
// Read the quantity from the address
$Quantity = $_GET['newQuantity'];
//Create a query for retreiving the product image
$queryGetImage = "SELECT [Image] FROM [Product Details] Where [Product ID]=" . $Pid;
//execute the query
$rs = $conn->execute($queryGetImage);
echo "<h2> Add product to basket </h2>";
echo "</br></br>";
echo $rs->Fields("Image");
echo "</br></br>";
//declare the form
?>
<FORM NAME ="QuantityForm" METHOD ="get" ACTION = "">
Product ID:</br> <INPUT TYPE = "TEXT" NAME ="newPid" VALUE = "<?php echo $Pid;?>">
</br></br>
Quantity:</br> <INPUT TYPE = "TEXT" NAME ="newQuantity">
</br></br>
<input type="submit" />
</form>
<?php
//Create the AddToBasket query
if (isset($Quantity)) {
// Add the product and the quantity to the basket table
$AddtoBasketQuery = "INSERT INTO Basket([Product ID], [Quantity])
VALUES ('$Pid', '$Quantity')";
//execute the query
$conn->execute($AddtoBasketQuery);
//close the connection
$conn->Close();
$conn = null;
echo "</br> Your product has succesfully been added to your basket.";
}
?>
答案 0 :(得分:0)
我没看到
session_start();
在第二个脚本上,您需要将它放在每个想要使用会话变量的脚本的顶部
答案 1 :(得分:0)
您需要添加session_start();在第二个脚本开始。要访问Session变量,需要在执行任何操作之前启动会话。