function updateInventory($connection, $books) { foreach ($books as $isbn => $quantity) { $old_quantity = getQuantity($connection, $isbn); if ($old_quantity < $quantity) { $missing = $quantity - $old_quantity; if ($missing == 1) { // TODO: Add links to the missing books. echo "\n <p class='center'>There lacks 1 copy of book with\n <em><strong>ISBN {$isbn}</strong></em>.</p>"; } else { echo "\n <p class='center'>There lack {$missing} copies of book with\n <em><strong>ISBN {$isbn}</strong></em>.</p>"; } return false; } $new_quantity = $old_quantity - $quantity; $query = "UPDATE inventory SET quantity = {$new_quantity}" . " WHERE isbn = '{$isbn}';"; $result = mysql_query($query, $connection); if (!$result) { echo "<p class='center'>Failed to retrieve books from inventory.</p>"; return false; } } return true; }
/** * Updates the quantity of an item. * * @param $db A DB object for secure database operations * @param $pid The product ID */ function updateItem($db, $pid) { $cartID = getCartID(); $qty = getQuantity(); if ($qty <= 0) { deleteItem($db, $pid); } else { $sql = "UPDATE shoppingcarts SET Quantity={$qty} \n WHERE CartID='{$cartID}' AND ProductID={$pid}"; $db->query($sql); } }