Jump to content

Quantity in shopping basket add and subtract buttons not working


mik_se7

Recommended Posts

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.

$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"];

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.