Jump to content

Possible Session issue


chet139

Recommended Posts

Hi All,

 

I may have no alternative but to post all the code so here it is - apologies.

 

<?php
require_once ('dbcon.html');//connect to db
include("db.php");
session_start();

if (isset($_GET["action"]))
{
	switch($_GET["action"])
	{
		case "add_item":
		{
			AddItem($_GET["id"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "update_item":
		{
			UpdateItem($_GET["id"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "remove_item":
		{
			RemoveItem($_GET["id"]);
			ShowCart();
			break;
		}
		default:
		{
			ShowCart();
		}
	}
}
else 
{
ShowCart();
}
function AddItem($productId, $qty)
{
	// Will check whether or not this item
	// already exists in the cart table.
	// If it does, the UpdateItem function
	// will be called instead
	// Check if this item already exists in the users cart table
	$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and productId = $productId");
	$row = mysql_fetch_row($result);
	$numRows = $row[0];

	if($numRows == 0)
	{
		// This item doesn't exist in the users cart,
		// we will add it with an insert query

		@mysql_query("insert into cart(cookieId, productId, qty) values('" . GetCartId() . "', $productId, $qty)");
	}
	else
	{
		// This item already exists in the users cart,
		// we will update it instead

		UpdateItem($productId, $qty);
	}
}

function UpdateItem($productId, $qty)
{
	// Updates the quantity of an item in the users cart.
	// If the qutnaity is zero, then RemoveItem will be
	// called instead	
	if($qty == 0)
	{
		// Remove the item from the users cart
		RemoveItem($productId);
	}
	else
	{
		mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and productId = $productId");
	}
}

function RemoveItem($productId)
{
	// Uses an SQL delete statement to remove an item from
	// the users cart
	mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and productId = $productId");

}

function ShowCart()
{

	// Gets each item from the cart table and display them in
	// a tabulated format, as well as a final total for the cart

	$totalCost = 0;
	$result = mysql_query("select * from cart inner join product on cart.productId = product.productId where cart.cookieId = '" . GetCartId() . "' order by product.productMake asc");
	?>
	<html>
	<head>
	<title>Order Contents </title>
	<script language="JavaScript">

		function UpdateQty(product)
		{
			productId = product.name;
			newQty = product.options[product.selectedIndex].text;

			document.location.href = 'cart.php?action=update_item&id='+productId+'&qty='+newQty;
		}

	</script>
	</head>
	<body bgcolor="#ffffff">
	<h1>Order Contents</h1>
	<form name="frmCart" method="get">
	<table width="100%" cellspacing="0" cellpadding="0" border="0">
		<tr>
			<td width="5%" height="25" bgcolor="red">
				<font face="verdana" size="1" color="white">
					  <b>Qty</b>
				</font>
			</td>
			<td width="5%" height="25" bgcolor="red">
				<font face="verdana" size="1" color="white">
					<b>Product ID</b>
				</font>
			</td>
			<td width="5%" height="25" bgcolor="red">
				<font face="verdana" size="1" color="white">
					<b>Product</b>
				</font>
			</td>
			<td width="5%" height="25" bgcolor="red">
				<font face="verdana" size="1" color="white">
					<b>Price Each</b>
				</font>
			</td>
			<td width="5%" height="25" bgcolor="red">
				<font face="verdana" size="1" color="white">
					<b>Remove?</b>
				</font>
			</td>
		</tr>
		<?php

		while($row = mysql_fetch_array($result))
		{
			// Increment the total cost of all items
			$totalCost += ($row["qty"] * $row["retailPrice"]);
****************$_SESSION['totalCost'] = $totalCost; *********************

			?>
				<tr>
					<td width="5%" height="25">
						<font face="verdana" size="1" color="black">
							<select name="<?php echo $row["productId"]; ?>" onChange="UpdateQty(this)">
							<?php

								for($i = 1; $i <= 20; $i++)
								{
									echo "<option ";
									if($row["qty"] == $i)
									{
										echo " SELECTED ";
									}
									echo ">" . $i . "</option>";
								}
							?>
							</select>
						</font>
					</td>

					<td width="5%" height="25">
						<font face="verdana" size="1" color="black">
							<?php echo $row["productId"]; ?>
						</font>
					</td>

					<td width="5%" height="25">
						<font face="verdana" size="1" color="black">
							<?php echo $row["productMake"]; ?>
						</font>
					</td>
					<td width="5%" height="25">
						<font face="verdana" size="1" color="black">
							£<?php echo number_format($row["retailPrice"], 2, ".", ","); ?>
						</font>
					</td>
					<td width="5%" height="25">
						<font face="verdana" size="1" color="black">
							<a href="cart.php?action=remove_item&id=<?php echo $row["productId"]?>">Remove</a>
						</font>
					</td>
				</tr>
			<?php
		}
		// Display the total
		?>
				<tr>
					<td width="100%" colspan="4">
						<hr size="1" color="red" NOSHADE>
					</td>
				</tr>
				<tr>
					<td width="70%" colspan="2">
						<font face="verdana" size="1" color="black">
							<a href="getallProduct.php"><< Products List</a>
						</font>
					</td>
					<td width="70%" colspan="2">
						<font face="verdana" size="1" color="black">
							<a href="indOrder2.php?custId=<?php echo $_SESSION["custId"]?>&orderId=<?php echo $_SESSION["orderId"]?>&userId=<?php echo $_SESSION["userId"]?>"><< Order Page</a>
						</font>
					</td>
					<td width="30%" colspan="2">
						<font face="verdana" size="2" color="black">
							<b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?></b>
						</font>
					</td>
				</tr>
			</table>
			</form>
			<?php echo $_SESSION['totalCost'];//PUT IN TO SEE WHATS STORED IN THIS SESSION VAR?> 

		</body>
		</html>
<?php}

?>

 

I am using this cart code - I attempted to store the total in the session variable(highlighted with ****)  - however when the cart is emptied the session variable still holds the total. How can I fix this. The last PHP echo line is there for debugging purposes so I can see the contents of $_SESSION['totalCost'].

Link to comment
Share on other sites

I may have missed it (there was a long bit of code there)...but in the only location I see anything removed (removeItem), you remove only from the database...not from the session...you would actually need to remove from the session as well in order for the session variable to update.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.