I have a couple of scripts to make a shopping system using sessions. Now for some reason on the second script, which is the product list, all the session variables are being emptied. I tried echoing the variables in different parts of the script and it will echo them only before the while loop I have setup to display the items from the database.
I have an include script called db.php.
Here are the scripts. I really am stumped by this, I don't know why this is happening at all.
db.php
<?php
// This page contains the connection routine for the
// database as well as getting the ID of the cart, etc
$dbServer = "server";
$dbUser = "user";
$dbPass = "pass";
$dbName = "db";
function ConnectToDb($server, $user, $pass, $database)
{
// Connect to the database and return
// true/false depending on whether or
// not a connection could be made.
$s = mysql_connect($server, $user, $pass);
$d = mysql_select_db($database, $s);
if(!$s || !$d)
return false;
else
return true;
}
function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID
session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
?>
And this is product.php
<?php
// This page will list all of the items
// from the product_list table. Each item will have
// a link to add it to the cart
include("db.php");
// Get a connection to the database
$cxn = ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$_SESSION["country"] = $_POST["selcountry"];
if ($_SESSION["country"] != "USA")
{
$pagetitle = "International Product List";
$table = "foreign_prodlist";
}
else
{
$pagetitle = "United States Product List";
$table = "product_list";
}
$result = mysql_query("select * from " . $table . " order by itemId asc");
$_SESSION["table"] = $table;
echo $_SESSION["table"];
echo $_SESSION["country"];
?>
<html>
<font face="verdana" size="3" color="black"><br>
<?php echo $pagetitle; ?>
</font>
<body>
<table>
<tr>
<th><font face="verdana" size="2" color="black">Name</font></th>
<th><font face="verdana" size="2" color="black">Price</font></th>
<th><font face="verdana" size="2" color="black">Description</font></th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="30%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo $row["itemPrice"]; ?>
</font>
</td>
<td width="50%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemDesc"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=add_item&id=<?php echo $row['itemId']; ?>&qty=1">Add Item</a>
</font>
</td>
</tr>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<?php } ?>
<tr>
<td width="100%" colspan="4">
<font face="verdana" size="1" color="black">
<a href="cart.php">Your Shopping Cart >></a>
</font>
</td>
</tr>
</table>
</body>
</html>
Any help would be greatly appreciated!