php实现购物车功能(上)

2019-12-13 13:11:54于海丽

   

已填写好信息的订单


获取客户信用卡信息

由以下代码实现:
5.7 purchase.php

 

<?php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 从用户获取付款细节 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
 require_once('book_sc_fns.php'); 
 
 session_start(); 
 
 do_html_header("Checkout"); 
 
 //创建变量 
 $name = $_POST['name']; 
 $address = $_POST['address']; 
 $city = $_POST['city']; 
 $zip = $_POST['zip']; 
 $country = $_POST['country']; 
 
 //如果订单细节填满 
 if(($_SESSION['cart']) && ($name) && ($address) && ($city) && ($zip) && ($country)) 
 { 
 if(insert_order($_POST) != false) 
 { 
  display_cart($_SESSION['cart'],false,0); 
  
  display_shipping(calculate_shipping_cost()); 
  
  display_card_form($name); 
  
  display_button("show_cart.php","continue-shopping","Continue Shopping"); 
 } 
 else 
 { 
  echo "<p>Could not store data, please try again.</p><hr/>"; 
  display_button('checkout.php','back','Back'); 
 } 
 } 
 else 
 { 
 echo "<p>You did not fill in all the fields, please try again.</p><hr/>"; 
 display_button('checkout.php','back','Back'); 
 } 
 do_html_footer(); 
?> 

5.8 order_fns.php文件中的函数insert_order()

 

function insert_order($order_details) //提取订单细节作为变量 
 { 
 extract($order_details); 
  
 //设置邮寄地址为当前地址 
 if((!$ship_name) && (!$ship_address) && (!$ship_city) && (!$ship_state) && (!$ship_zip) &&(!$ship_country)) 
 { 
  $ship_name = $name; 
  $ship_address = $address; 
  $ship_city = $city; 
  $ship_state = $state; 
  $ship_zip = $zip; 
  $ship_country = $country; 
 } 
  
 //连接数据库 
 $conn = db_connect(); 
  
 //事务开始,必须关闭自动提交 
 $conn ->autocommit(false); 
  
 $query = "select customrid from customers where 
   name ='". $name ."' and address = '". $address ."' 
   and city = '". $city ."' and state = '". $state ."' 
   and zip = '". $zip ."' and country = '". $country ."'"; 
   
 $result = $conn ->query($query); 
  
 if(@$result ->num_rows > 0) 
 { 
  $customer = $result ->fetch_object(); 
  $customerid = $customer ->customerid; 
 } 
 else 
 { 
  $query = "insert into customers values 
   ('','". $name ."','". $address ."','". $city ."','". $state ."','". $zip ."','". $country ."')"; 
  $result = $conn ->query($query); 
  
  if(!$result) 
  return false; 
 } 
  
 $customerid = $conn ->insert_id; //返回上次查询中自增量的ID 
  
 $date = date("Y-m-d"); 
  
 $query ="insert into orders values 
  ('','". $customerid ."','". $_SESSION['total_price'] ."','". $date ."','PARTIAL','". $ship_name ."','". $ship_address ."','". $ship_city ."','". $ship_state ."','". $ship_zip ."','". $ship_country ."')"; 
   
 $result = $conn ->query($query); 
 if(!$result) 
  return false; 
  
 $query = "select orderid from orders where 
   customerid ='". $customerid ."' and 
   amount > (". $_SESSION['total_price'] ."-.001) and 
   amount < (". $_SESSION['total_price'] ."+.001) and 
   date ='". $date ."' and 
   order_status = 'PARTIAL' and 
   ship_name ='". $ship_name ."' and 
   ship_address ='". $ship_address ."' and 
   ship_city ='". $ship_city ."' and 
   ship_state ='". $ship_state ."' and 
   ship_zip ='". $ship_zip ."' and 
   ship_country ='". $ship_country ."'"; 
  
 $result = $conn ->query($query); 
  
 if($result ->num_rows > 0) 
 { 
  $order = $result ->fetch_object(); 
  $orderid = $order ->orderid; 
 } 
 else 
  return false; 
  
 foreach($_SESSION['cart'] as $isbn => $quantity) 
 { 
  $detail = get_book_details($isbn); 
  $query = "delete from order_items where 
   orderid = '". $orderid ."' and isbn = '". $isbn ."'"; 
  $result = $conn ->query($query); 
  
  $query = "insert into order_items values 
   ('". $orderid ."','". $isbn ."',". $detail['price'] .",$quantity)"; 
  $result = $conn ->query($query); 
  if(!$result) 
  return false; 
 } 
  
 //事务关闭,开启自动提交 
 $conn ->commit(); 
 $conn ->autocommit(true); 
  
 return $orderid; 
 } 

								 
			 
相关文章 大家在看