mik_se7 Posted November 19, 2014 Share Posted November 19, 2014 (edited) I have setup a website that has a shopping basket and it displays the objects ordered in a table before being purchased, i have added a plus and minus button to adjust the quantity using the following code: $add ="<form action='cart.php' method='post'><input type='hidden' name='additem' value='$id'/><input type='submit' name='itemadd' onclick='CheckQty()' value='+'/></form>"; $sub ="<form name='sub' action='cart.php' method='post'><input type='hidden' name='subitem' value='$id'/><input type='submit' name='itemsub' value='-'/></form>"; //add to quantity of item if (isset($_POST['itemadd'])){ $additem = $_POST['additem']; if ($qty < 5){ $addqty = $qty + 1; $adding = "UPDATE orders SET quantity = '$addqty' WHERE id = '$additem'"; $add_query = mysqli_query($db_conx, $adding); header('Location: http://www.quichlicious.co.uk/cart.php'); //print $additem; exit(); } else { $usemes = "You have reached maximum quantity"; } } //subtract to quantity of item if (isset($_POST['itemsub']) && ($_POST['itemsub'] !="")){ $subitem = $_POST['subitem']; if ($qty > 1){ $subqty = $qty - 1; $subing = "UPDATE orders SET quantity = '$subqty' WHERE id = '$subitem'"; $sub_query = mysqli_query($db_conx, $subing); header("location: cart.php"); exit(); } else { $usemes = "<font color='red'>Please use delete to remove item</font>"; } } The issue i have is it works great if just one item is in the basket but if i have more than 1 item in the basket i can only adjust the very last item in the baskets quantity and the others just randomly jump numbers or will not change at all? $additem gets the item ID from the table. Edited November 19, 2014 by mik_se7 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 19, 2014 Share Posted November 19, 2014 if ($qty < 5){ $addqty = $qty + 1; Where is $qty defined? Quote Link to comment Share on other sites More sharing options...
mik_se7 Posted November 19, 2014 Author Share Posted November 19, 2014 $qty is grabbed from the database using the query below done just before the add and subtract buttons in php tags $sqlfetch = "SELECT * FROM orders WHERE orderid = '$sessionid'"; $order_query = mysqli_query($db_conx, $sqlfetch); $numrows = mysqli_num_rows($order_query); if($numrows < 1){ $result .="Cart is Empty"; header("location: order.php"); exit(); } $result .= "<table><tr><th>id</th><th>Quiche Size</th><th>Pastry</th><th>Cheese</th><th>Toppings</th><th>order placed</th><th>Quantity</th><th>Price £</th><th>Remove</th></tr>"; while($row = mysqli_fetch_array($order_query, MYSQLI_ASSOC)){ $id = $row["id"]; $customerid = $row["orderid"]; $qsize = $row["size"]; $pastry = $row["pastry"]; $cheese = $row["cheese"]; $top1 = $row["top1"]; $top2 = $row["ordered"]; date_default_timezone_set('Europe/London'); $top2date = date("d-m-Y H:i:s", strtotime($top2)); $qty = $row["quantity"]; Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted November 19, 2014 Solution Share Posted November 19, 2014 How do you know which quantity belongs to which item, all you save is one $qty Quote Link to comment Share on other sites More sharing options...
mik_se7 Posted November 20, 2014 Author Share Posted November 20, 2014 Thanks Barand your question lead me on the correct path as you suggested it only had one $qty reference so i added a sql request to get the item quantity by id and gave it another $variable name and hey presto it now works perfectly. Michael Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.