Hi, The biggest problem with my shopping basket system I think is no cookies are appearing on my computer when I use a standard browser. So the coding which checks for cookies (or adds a cookie to a computer) is not being used!? The best way to explain my problem is to explain the pages that are being used. There may be another problem with my coding but I think the main problem is no cookies are appearing on my computer. I am using the code at http://www.devarticles.com/c/a/MySQL/Building-A-Persistent-Shopping-Cart-With-PHP-and-MySQL/1/ but I have edited it because it is not quite complete. --------------------------------------------------------------------------------------------------------------- I am using this link on a product page: <a href="showcart.php?action=add_item&productid=<?php echo $row["productid"]; ?>&quantity=1">Add Item</a> --------------------------------------------------------------------------------------------------------------- This is redirected to showcart.php: <?php include("cart.php"); $connect = @mysql_connect($server, $user, $pass); $d = @mysql_select_db($name, $connect); $result = mysql_query("select * from cart inner join products on cart.productid = products.productid where cart.cookieid = '" . GetCartId() . "' order by products.name asc"); //Once the list of items is retrieved, each item is displayed as part of a table, as a table row: while($row = mysql_fetch_array($result)) { // Increment the total cost of all items $totalCost = ($row["quantity"] * $row["price"]);?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <script language="JavaScript"> function UpdateQty(item) { itemId = item.name; newQty = item.options[item.selectedIndex].text; document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty; } </script> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <tr> <td width="15%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["productid"]; ?> <select name="<?php echo $row["productid"]; ?>" onChange="UpdateQty(this)"> <?php for($i = 1; $i <= 20; $i++) { echo "<option "; if($row["quantity"] == $i) { echo " SELECTED "; } echo ">" . $i . "</option>"; } ?> </select> </font> </td> <td width="55%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["name"]; ?> </font> </td> <td width="20%" height="25"> <font face="verdana" size="1" color="black"> $<?php echo number_format($row["price"], 2, ".", ","); ?> </font> </td> <td width="10%" 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 } //The running tally of each item in the users shopping cart is kept as the $totalCost variable: // Increment the total cost of all items $totalCost += ($row["quantity"] * $row["price"]); //The quantity of each item is multiplied by its price, and is then added to the $totalCost variable using the += operator, which is the same as: $totalCost = $totalCost + ($row["quantity"] * $row["price"]);?> "As I mentioned above, the quantity for each item is displayed in a drop down list. This list is generated with the following code: The select tag contains a trigger for the onChange event, which calls a JavaScript function called UpdateQty. UpdateQty is defined at the top of the ShowCart function, and looks like this: Once called, UpdateQty takes the quantity of the selected item as well as the items ID (which is the name of the drop down list) and passes them as query string variables to cart.php. For example, if I updated the quantity of a product whose ID was 5 to 4, then I would be redirected to the following URL: cart.php?action=update_item&id=5&qty=4 Lastly, ShowCart displays the total cost of all items in the users cart: <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="products.php"><< Keep Shopping</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> </body> </html> --------------------------------------------------------------------------------------------------------------- At the top of the coding for the ShowCart.php is a link to Cart.php: <?php // This page contains the connection routine for the // database as well as getting the ID of the cart, etc $server = "LOCATION"; $user = "USERNAME"; $pass = "PW"; $name = "ACCESSTHIS"; // Connect to the database and return // true/false depending on whether or // not a connection could be made. $connect = @mysql_connect($server, $user, $pass); $d = @mysql_select_db($name, $connect); if(!$connect || !$name){ 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["id"])) { return $_COOKIE["id"]; } else { // There is no cookie set. We will set the cookie // and return the value of the users session ID session_start(); setcookie("id", session_id(), time() + ((3600 * 24) * 4)); return session_id(); } } ?> --------------------------------------------------------------------------------------------------------------- And you'll notice that CartAction.php is utilised along the way. This file uses a switch to define cases. The functions are defines afterwards (later in the file). This might not be proper coding - can you confirm this coding will work as it is intended: <?php include("cart.php"); switch($_GET["action"]) { case "add_item": { AddItem($_GET["productid"], $_GET["quantity"]); ShowCart(); break; } case "update_item": { UpdateItem($_GET["productid"], $_GET["quantity"]); ShowCart(); break; } case "remove_item": { RemoveItem($_GET["productid"]); ShowCart(); break; } default: { ShowCart(); } } //Looking at the switch statement above, we have four possible cases, each of which is discussed below: //add_item: When the user clicks on the "Add Item" for an item on the products.php page, this case will be called. It calls //the AddItem function, passing in an items ID and quantity. //update_item: Updates the quantity of an item in the users shopping cart. As you will see shortly, each item in the cart //is displayed with a drop down list, that, when changed, automatically updates the numeric of a specific item in the users //shopping cart. //remove_item: Deletes an item from the cart table for the current user. //If the cart.php page is called up with no query string parameters, then the ShowCart function is called. Let's start by //looking at the AddItem function. //AddItem accepts two parameters: The ID of the item to add to the cart, and the number of that item to add: function AddItem($productid, $quantity){ //The main part of the AddItem function checks whether or not this item already exists in the users cart. If it does, then //its quantity field is updated and it isn't added again: $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, quantity) values('" . GetCartId() . "', $productid, $quantity)"); } else { } // This item already exists in the users cart, // we will update it instead UpdateItem($productid, $quantity); } //Looking at the code above, we can see that if $numRows equals zero (i.e. the item isn't already in the users cart) then //the item is added to the cart table. If not, the items quantity field is updated by calling the UpdateItem function, //which is described below. //UpdateItem accepts two parameters, in the same way that the AddItem function does: function UpdateItem($productid, $quantity){ //It executes a simple UPDATE SQL query against the cart table, updating the quantity of one specific item. The cookieId //field is used to match the users session ID to that particular product, making sure that the quantity is only updated for //that item and the current user: mysql_query("update cart set quantity = $quantity where cookieid = '" . GetCartId() . "' and productid = $productid"); //Removing an item is a simple matter of the RemoveItem function being called. It accepts just one parameter, which is the //ID of the item to delete: } function RemoveItem($productid){ //Once connected to the database, a simple SQL DELETE query removes the item from the current users cart: mysql_query("delete from cart where cookieid = '" . GetCartId() . "' and productid = $productid"); //All of these functions are good, but to actually call them, we need to look at the ShowCart function, which is what we //will do on the next page. } ?> --------------------------------------------------------------------------------------------------------------- Why are cookies not appearing on my computer? Why is no basket contents showing (I think this is because the cookieid is not matching and no content is found and so none is displayed)? Look forward to your help/advice, Matt.
I have tried this code ONLY in a web page and the cookie does not appear on the computer: <?php 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["cookieid"])) { return $_COOKIE["cookieid"]; } else { // There is no cookie set. We will set the cookie // and return the value of the users session ID session_start(); setcookie("cookieid", session_id(), time() + ((3600 * 24) * 4)); return session_id(); } } ?> I think there is nowhere asking for this function to be run so it does not run. Where should I ask it to run?? (what page within my previous post) Hopefully this might help resolve the problems I am having. Matt.