Jump to content

Quantity in shopping basket add and subtract buttons not working


mik_se7
Go to solution Solved by Barand,

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.

Edited by mik_se7
Link to comment
Share on other sites

$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"];
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.