0

I have a small, primitive shopping cart I am building for learning purposes (security is not an issue, this is just to help learn theory).

Right now I am trying to give functionality to updating the cart. At the moment, the following code works mostly:

 $sql = $connection->prepare("UPDATE cart SET quantity=? WHERE productID=?");
 foreach (array_combine($_POST['productID'], $_POST['quantity']) as $key => $value) {
     $sql->execute(array(
         $value,
         $key
     ));
 }

What I would like to do now, is sort by a third column. I have created a third item descriptor, zodiac sign, and I would like to sort through all three.

So based on the product chosen, it sends the Zodiac sign, quantity of the item purchased, and the productID of the item. My problem with this particular code is that I have some of the same products but with different zodiac signs. So if I change the quantity of those two items that have the same zodiac sign, it changes the quantity of both of them.

So I have the two arrays for productID, and quantity. Now I need one for zodiac. How can I then combine those 3 arrays, and send that data to the sql table?

1 Answer 1

1

You could something like this:

$sql = $connection->prepare(
        "UPDATE cart SET quantity=? WHERE productID=? AND zodiac_sign=?");
foreach ($_POST['productID'] as $i => $val) {
    $sql->execute(array(
        $_POST['quantity'][$i],
        $_POST['productID'][$i],
        $_POST['zodiac_sign'][$i]
    ));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.