Page 1 of 1

AddItem Function not adding items, RemoveItem not removing..

Posted: Thu Jun 17, 2004 10:32 am
by dardsemail
I am in a shopping cart nightmare. I have changed my code around so that its not quite as hellish as previously, but am now unable to add items or remove items from my cart.

Here's how the code is supposed to work:

The user clicks on an 'add item' link in the collections page. That link generates the following URL:

Code: Select all

<a href="cart.php?add_item&piecenum=<?php echo $row_rs_pieceinfoї'piecenum']; ?>&qty=1">Add Item</a></td>
So far so good. The cart page is being displayed and the URL structure appears correct, but the item is not being added to the cart. (I should note that if I add items manually to the DB they do appear in the table). Here's the code for the "include.php" page which is where the AddItem function resides:

Code: Select all

<?php
require_once('Connections/conn_my_db.php');?>
<?php
 error_reporting (E_ALL);
 
 include ("db.php");

  	switch(@$_GET["action"]) 

	{
		case "add_item":
		{
			AddItem($_GET["piecenum"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "update_item":
		{
			UpdateItem($_GET["piecenum"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "remove_item":
		{
			RemoveItem($_GET["piecenum"]);
			ShowCart();
			break;
		}
		default:
		{
			ShowCart();
		}
	}

 
	function AddItem($piecenum, $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 piecenum = $piecenum"); 
		$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, piecenum, qty) VALUES('" . GetCartId() . "', $piecenum, $qty)");
		}
		else
		{
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($piecenum, $qty);
		}
	}
	
	function UpdateItem($piecenum, $qty)
	{
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead


		// Get a connection to the database
		
		if($qty == 0)
		{
			// Remove the item from the users cart
			RemoveItem($piecenum);
		}
		else
		{
			mysql_query("UPDATE cart SET qty =  $qty WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
		}
	}
	
	function RemoveItem($piecenum)
	{
		// Uses an SQL delete statement to remove an item from
		// the users cart
		
		mysql_query("DELETE FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
	}
	
function ShowCart()
{
		$hostname = 'localhost'; 
		$username ='abc'; 
		$password ='123'; 
		$dbName = 'my_db'; 

		MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect"); 
		@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database"); 
			
$ShowCart = mysql_query("SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'");
$row_ShowCart = mysql_fetch_assoc($ShowCart);

	while($row_ShowCart = mysql_fetch_array($ShowCart))
	{
	echo "\n<tr>";
	echo "\n<td>";
	echo "\n</td>";
	echo "\n<td>";
	echo ($row_ShowCart['description']);
	echo "\n</td>";
	echo "\n<td>";
	echo ($row_ShowCart['price']);
	echo "\n</td>";
	echo "\n<td>";
	echo "<a href=cart.php?action=remove_item&piecenum=",($row_ShowCart['piecenum']),">Remove</a>";
	echo "\n</td>";
	}
}
?>
Here is the code for the "db.php" where the sessionID is set:

Code: Select all

<?php

// This page contains the connection routine for the 
// database as well as getting the ID of the cart, etc. 

# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_conn_my_db = "localhost";
$database_conn_my_db = "my_db";
$username_conn_my_db = "abc";
$password_conn_my_db = "123";
$conn_fjdesigns_db = mysql_pconnect($hostname_conn_my_db, $username_conn_my_db, $password_conn_my_db) or trigger_error(mysql_error(),E_USER_ERROR);

session_start();
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 
setcookie("cartID", session_id(), time() + ((3600 * 24) * 30)); 
return session_id(); 
} 
}
?>
Here is the code from the header of the cart.php page that displays the data (stripped of excess html):

Code: Select all

<?php

ob_start();
	
include ("include.php");

 error_reporting (E_ALL);

?>
Here is the code from the body of the page where the ShowCart function is called:

Code: Select all

<?php
			  ShowCart();


?>
My problem right now is that I just cannot seem to get the add or remove functions working. I haven't even begun to try the update, but figured that I'd do this in steps. Is it that the functions aren't being called anywhere, because from what I can see they are. Additionally, I should note that I have run these through a debugger and they seem to run clean.

Any help would be greatly appreciated. This shopping cart function has become my nemesis!

Posted: Thu Jun 17, 2004 10:34 am
by markl999
I notice you do a switch on $_GET['action'], so shouldn't the url be:
<a href="cart.php?action=add_item&piecenum... ?

Posted: Thu Jun 17, 2004 10:36 am
by dardsemail
Good eye - that was a typo from me mucking around with the code. I changed it - but still no go.

Posted: Thu Jun 17, 2004 10:41 am
by magicrobotmonkey
you need to drop some of those @'s and see what errors you are getting. it doesnt do any good to turn on error reporting if you turn around and supress them with an @

Posted: Thu Jun 17, 2004 10:43 am
by markl999
I'd start adding some debugging code.
Add mysql_error() to all your mysql_* functions, eg:
mysql_query($foo) or die(mysql_error());

Also add some echo lines, like echo 'Debug 1'; then echo 'Debug 2'; at various points, you should then be able to track the code flow and see where it's getting to and where it's failing. At a glance i can't see anything that jumps out as obviously wrong :o

Posted: Thu Jun 17, 2004 10:52 am
by dardsemail
Ah-ha - now we're getting there. I removed the @ and discovered the a Cannot send session header... error. So, I added a ob_start(); to the top of the include.php file and now the data is appearing...

However, I have one small problem. Its appearing in the header of the page as well as in the body. (But I am able to add and remove items!)

Any thoughts as to why its appearing in both places?

Posted: Thu Jun 17, 2004 10:58 am
by markl999
The session error should say something like 'output start in foo.php at line blah'
Find line blah in foo.php and see why it's causing output (which causes the session error as you can't have any output before session_start, the output could easily be an error from the line you find).