prepare($qry); $params = array("pid" => $pid); $stmt->execute($params); $row = $stmt->fetch(); if ($row) { $name = $row["name"]; $qry = "SELECT qty FROM MyCart WHERE pid=:pid"; $stmt = $db->prepare($qry); $stmt->execute($params); $cart_row = $stmt->fetch(); header("Content-type: application/json"); if ($cart_row) { $old_qty = $cart_row["qty"]; $qty += $old_qty; $qry = "UPDATE MyCart SET qty=:qty, lastupdated=NOW() WHERE pid=:pid"; } else { # No product found with the given $pid $qry = "INSERT INTO MyCart (pid, qty, lastupdated) VALUES(:pid, :qty, NOW())"; } $stmt = $db->prepare($qry); $params = array("qty" => $qty, "pid" => $pid); $stmt->execute($params); print(json_encode(array("success" => "{$qty} of {$name} added to your shopping cart!"))); } else { handle_error("Product not found in our inventory"); } } catch (PDOException $ex) { handle_error("Error adding product into database. Please try again later.", $ex); } } ?>