buy.php This page completes the purchase function
The main purpose here is to process the purchase of goods in the session, and combine the product information in the session with the purchased product information Compared.
If it is the first time to purchase an item, add the product information to the shopping cart and calculate the total price.
If you click to purchase again, the quantity of purchased items will be increased by 1, and the total price will be calculated. To recalculate, view the shopping cart link to the shopping cart page.
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>购买页</title> </head> <body> <?php //开启session session_start(); //获取传过来的商品名和价格 $name = $_GET['name']; $price = $_GET['price']; //把session中的商品信息和传过来的(刚买的)商品信息对比 $goods = $_SESSION['goods']; if ($name == $goods[$name]['name']) { //买过的话,则总价格增加,相应商品数量增加 $_SESSION['totalPrice'] += $price; $goods[$name]['number'] += 1; } else { //第一次买的话,将相应的商品信息添加到session中 $goods[$name]['name'] = $name; $goods[$name]['price'] = $price; $goods[$name]['number'] += 1; $_SESSION['totalPrice'] += $price; } $_SESSION['goods'] = $goods; //购买处理完毕后跳转到商品列表 header('location: list.php'); ?> </body> </html>