I am trying to use some code I adapted some time ago. It is from some shopping cart code. The original program had a function called GetCartId() that set a cookie while someone was “shopping” on the products page.
I am trying to adapt it to “call” the information from the mySQL tables and allow some of the same, original, shopping cart functions of removing items from the cart and updating the quantities for a given line item.
For this adaptation, the original cartId for an order is put in a form and all entries for the matching cartID are shown. However, when you try to update or remove an item from the order, the process fails. So the ShowOrder() function in the below code works correctly, however, RemoveItem() and UpdateItem(), whcih then calls the same file again (just like the original cart code), does not work correctly. I have tried using sessions and setting new cookies to no avail.
I cannot figure out what is wrong. I know it is probably stupid but I have looked and looked with no luck. I would take some troubleshooting advice as well. Thanks.
Here is the code showing the working ShowOrder() and the ones that fail (many of the $ variable values come from the dp.php file):
<?php session_start(); include("db.php"); $cookie = $_GET["cookie"]; $_SESSION['cookie'] = $cookie; $action = ''; if (isset($_GET['action'])) $action = $_GET['action']; switch($action) { case "add_item": { AddItem($_GET["id"], $_GET["qty"]); ShowOrder(); break; } case "update_item": { UpdateItem($_GET["id"], $_GET["qty"]); ShowOrder(); break; } case "remove_item": { RemoveItem($_GET["id"]); ShowOrder(); break; } default: { ShowOrder(); } } function AddItem($itemId, $qty) { // Will check whether or not this item // already exists in the cart table. // If it does, the UpdateItem function // will be called instead global $dbServer, $dbUser, $dbPass, $dbName; global $itemtable, $carttable; // Get a connection to the database $cxn = @mysqli_connect($dbServer, $dbUser, $dbPass, $dbName); // Check if this item already exists in the users cart $result = mysqli_query($cxn, "select count(*) from $carttable where cookieId = '" . $GLOBALS['cookie'] . "' and itemId = $itemId"); $row = mysqli_fetch_row($result); $numRows = $row[0]; if($numRows == 0) { // This item doesn't exist in the users cart, // we will it with an insert query @mysqli_query($cxn, "insert into $carttable(cookieId, itemId, qty) values('" . $GLOBALS['cookie'] . "', $itemId, $qty)"); } else { // This item already exists in the users cart, // we will update it instead UpdateItem($itemId, $qty); } } function UpdateItem($itemId, $qty) { // Updates the quantity of an item in the users cart. // If the qutnaity is zero, then RemoveItem will be // called instead global $dbServer, $dbUser, $dbPass, $dbName; global $itemtable, $carttable; // Get a connection to the database $cxn = @mysqli_connect($dbServer, $dbUser, $dbPass, $dbName); if($qty == 0) { // Remove the item from the users cart RemoveItem($itemId); } else { mysqli_query($cxn, "update $carttable set qty = $qty where cookieId = '" . $GLOBALS['cookie'] . "' and itemId = $itemId"); } } function RemoveItem($itemId) { // Uses an SQL delete statement to remove an item from // the users cart global $dbServer, $dbUser, $dbPass, $dbName; global $carttable, $itemtable; // Get a connection to the database $cxn = @mysqli_connect($dbServer, $dbUser, $dbPass, $dbName); mysqli_query($cxn, "delete from $carttable where cookieId = '" . $GLOBALS['cookie'] . "' and itemId = $itemId"); } function ShowOrder() { // Gets each item from the cart table and display them in // a tabulated format, as well as a final for the cart global $dbServer, $dbUser, $dbPass, $dbName; global $carttable, $itemtable, $cartheader, $carttitle, $headercolor; global $contshop, $shoppay, $removeitem, $carttottext; // Get a connection to the database $cxn = @mysqli_connect($dbServer, $dbUser, $dbPass, $dbName); $result = mysqli_query($cxn, "select * from $carttable inner join $itemtable on $carttable.itemId = $itemtable.itemId where $carttable.cookieId = '" . $GLOBALS['cookie'] . "' order by $itemtable.itemId asc"); if (!$result){ echo("Error description: " . mysqli_error($cxn)); } ?>