Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP shopping cart display issue
    primarykey
    data
    text
    <p>When creating a shopping cart for my website, I have add and remove functions for individual products. When I add a product it adds it to a new row in the table, not the current row.</p> <p>Here is the code for adding and removing products:</p> <pre><code>function addproduct($product_id, $product_qty){ $q = "SELECT p_name FROM Product WHERE product_id = $product_id LIMIT 1"; $result = mysqli_query($_SESSION['conn'],$q); $row = mysqli_fetch_array($result); $product_name = $row['p_name']; //get the product name from product id because it is better to display name than id in the cart if (isset($_SESSION['cart'])){ //if shopping cart is not empty $cart = $_SESSION['cart']; if (array_key_exists($product_name, $cart)){ //if the product exists, update quantity $cart[$product_name] += $product_qty; } else { //otherwise, add new product-quantity pair to the array $cart[$product_name] = $product_qty; } $_SESSION['cart'] = $cart; //write the updated array back to session variable } else { //if shopping cart is empty $cart = array($product_name=&gt;$product_qty); //add product and quantity to the shopping cart $_SESSION['cart'] = $cart; //write the updated array back } mysqli_free_result($result); } function deleteproduct($product_id, $product_qty){ $q = "SELECT p_name FROM Product WHERE product_id = $product_id LIMIT 1"; $result = mysqli_query($_SESSION['conn'],$q); $row = mysqli_fetch_array($result); $product_name = $row['p_name']; if (isset($_SESSION['cart'])){ //if shopping cart is not empty $cart = $_SESSION['cart']; if (array_key_exists($product_name, $cart)){ //if product exists, update quantity $cart[$product_name] -= $product_qty; if ($cart[$product_name] == 0){ //if the qty 0, delete key unset($cart[$product_name]); } } else { //exception echo "&lt;p&gt;Error1&lt;/p&gt;"; } $_SESSION['cart'] = $cart; //write array back to session variable } else { echo "&lt;p&gt;Error2&lt;/p&gt;"; } mysqli_free_result($result); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload