Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Showcart () chức năng có chứa một liên kết để delete.php, trong đó bạn có thể loại bỏ một mục khỏi giỏ hàng. Bằng cách nhấn vào liên kết, mục được lấy ra từ bảng orderitems, và tổng giá trong bảng đơn đặt hàng được cập nhật. Tạo delete.php và bắt đầu thêm các mã: | CHAPTER 6 Creating a Shopping Cart 195 Deleting Items The showcart function contains a link to delete.php in which you can remove an item from the shopping cart. By clicking the link the item is removed from the orderitems table and the total price in the orders table is updated. Create delete.php and begin adding the code php require config.php require db.php require functions.php validid pf_validate_number _GET id redirect config_basedir . showcart.php itemsql SELECT FROM orderitems WHERE id . _GET id . itemres mysql_query itemsql numrows mysql_num_rows itemres if numrows 0 header Location . config_basedir . showcart.php itemrow mysql_fetch_assoc itemres In this code the query pulls the item from the orderitems table and the number of rows returned is checked. This check prevents someone modifying the URL and adding delete.php id 73 if there is no item with an id of 73. If no rows are returned a header redirect jumps to showcart.php. If a row is returned the script continues itemrow mysql_fetch_assoc itemres prodsql SELECT price FROM products WHERE id . itemrow product_id . prodres mysql_query prodsql prodrow mysql_fetch_assoc prodres sql DELETE FROM orderitems WHERE id . _GET id mysql_query sql In this block the price of the product is selected first and then a separate query removes the item from orderitems. Update the orders table with the new total price 196 Practical PHP and MySQL mysql_query sql totalprice prodrow price itemrow quantity updsql UPDATE orders SET total total - . totalprice . WHERE id . _SESsIoN SESS_ORDERNUM . mysql_query updres header Location . config_basedir . showcart.php With the cart summary function and pages complete your browser should show something similar to the page shown in Figure 6-6. FIGURE 6-6 The shopping cart summary displays a current list of items and the ability to remove them. Checking It Out After the user has finished adding items to his shopping cart the checkout process can begin. This process involves two steps .