Page 1 of 1

Shopping Cart; session issues?

Posted: Fri Jun 29, 2007 1:54 pm
by the9ulaire
Still working through my book. Today I'm in a chapter on online stores. Problem is, my cart isn't working at all. Every time I try to "add" something to my cart, it isn't truly added. I'm wondering if this is because cart.php cannot find a session_id.

My MySQL table is storing the information about what people are adding to their carts. I'm assuming my issue lies in the fact that it's having a problem retrieving the current session value. Is that correct?

Here's my "shop": http://lwrussell.com/phptesting/comicsi ... bashop.php

Here's my code for cart.php:

Code: Select all

<?php
if (!session_id()) {
	session_start();
}
require_once 'conn.php';
?>
<html>
<head>
<title>Here is your shopping cart!</title>
</head>
<body>
<div align="center">
You currently have
<?php
$sessid = session_id();

//display number of products in cart
$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
$results = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($results);
echo $rows;
?>

product(s) in your cart.<br />

<table border="1" align="center" cellpadding="5">
	<tr>
		<td>Quantity</td>
		<td>Item Image</td>
		<td>Item Name</td>
		<td>Price Each</td>
		<td>Extended Price</td>
		<td></td>
		<td></td>
	</tr>
<?php
$total = 0;
while ($row = mysql_fetch_array($results)) {
	echo "<tr>";
	extract($row);
	$prod = "SELECT * FROM products WHERE products_prodnum='$carttemp_prodnum'";
	$prod2 = mysql_query($prod);
	$prod3 = mysql_fetch_array($prod2);
	extract($prod3);
	echo "<td>
		<form method=\"post\" action=\"modcart.php?action=change\">
			<input type=\"hidden\" name=\"modified_hidden\" value=\"$carttemp_hidden\">
			<input type=\"text\" name=\"modified_quan\" size=\"2\" value=\"$carttemp_quan\">";
	echo "</td>";
	echo "<td>";
	echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
	echo "THUMNAIL<br />IMAGE</a></td>";
	echo "<td>";
	echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
	echo $products_name;
	echo "</td>";
	echo "<td aligh=\"right\">";
	//get extended price
	echo $extprice;
	echo "</td>";
	echo "<td>";
	echo "<input type=\"submit\" name=\"Submit\" value=\"Change Qty\"></form></td>";
	echo "<td>";
	echo "<form method=\"post\" action=\"modcart.php?action=delete\">
			<input type=\"$carttemp_hidden\" name=\"modified_hidden\" value=\"$carttemp_hidden\">";
	echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"></form></td>";
	echo "</tr>";
	//add extended price to total
	$total = $extprice + $total;
}
?>
	<tr>
		<td colspan="4" align="right">
			Your total before shipping is:</td>
		<td align="right"><?php echo number_format($total, 2); ?></td>
		<td></td>
		<td>
<?php
echo "<form method=\"post\" action=\"modcart.php?action=empty\">
		<input tye=\"hidden\" name=\"carttemp_hidden\" value=\"";
if (isset($carttemp_hidden)) {
	echo $carttemp_hidden;
}
echo "\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Empty Cart\"></form>";
?>
</td>
</tr>
</table>
<form method="post" action="checkout.php">
<input type="submit" name="Submit" value="Proceed to Checkout" />
</form>
<a href="cbashop.php">Go back to the main page</a>
</div>
</body>
</html>


Thanks for any help!
Luke