简体   繁体   English

如何将产品插入购物车然后注册或登录

[英]How to insert product to shopping cart then register or login

I have product , orderdetails and customer tables 我有产品,订单详情和客户表
the orderdetails table have products_id (FK) customers_id(FK) Quantity and size when customer is logged in and add items the items are inserted in the orderdetails table I want to allow the customer to add items/products to cart even without login then they can either login or register orderdetails表有products_id (FK) customers_id(FK)客户登录时的Quantitysize ,并添加项目在orderdetails表中插入的项目我想让客户即使没有登录也可以将商品/产品添加到购物车然后他们可以登录或注册
Do I need another table for that ? 我还需要另一张桌子吗?

Database 数据库
productTable (products_id(pk), products_name , price,Quantity ,size )
customerTable (customers_id(pk), cust_name, cust_email )
orderdetails (products_id(FK), customers_id(FK), price,Quantity,size )

     <?php
include'config.php'; 
  session_start();  

if(isset($_SESSION['product_name']) && !empty($_SESSION['product_name'])) {
 $products_name =$_SESSION['product_name'];
 }
if(isset($_SESSION['products_price']) && !empty($_SESSION['products_price'])) {
 $products_price =$_SESSION['products_price'];
}


 if(isset($_SESSION['Quantity']) && !empty($_SESSION['Quantity'])) {
  $Quantity = $_SESSION['Quantity'];

 } 
  if(isset($_SESSION['product_id']) && !empty($_SESSION['product_id'])) {
  $product_id =   $_SESSION['product_id'];
 }
   if(isset($_SESSION['customers_id']) && !empty($_SESSION['customers_id'])) {
         $customers_id =   $_SESSION['customers_id'];

   } 
    if(isset($_SESSION['size']) && !empty($_SESSION['size'])) {
       $size =   $_SESSION['size'];

   } 
 $query = "INSERT INTO orderdetails  (products_id,customers_id,price,Quantity ,size) VALUES ('$product_id','$customers_id','$products_price','$Quantity','$size')";
mysqli_query($conn,$query) ;
    mysqli_close($conn);




?>  

Not necessarily. 不必要。 If customers_id is not a foreign key or part of, you could simply generate a new customer_id for each user not logged in and use this one. 如果customers_id不是外键或其中的一部分,则只需为未登录的每个用户生成一个新的customer_id并使用该customer_id。 As soon as the user then logs in, you'd update the table: 一旦用户登录,您就会更新表格:

UPDATE orderdetails SET customers_id = <id of user logged in>
    WHERE customers_id = <previous temporary id>

If the session expires, you would delete any entries again (a shopping cart of a user not logged in cannot survive expired sessions, of course): 如果会话过期,您将再次删除任何条目(当然,未登录的用户的购物车无法在过期的会话中存活):

DELETE FROM orderdetails WHERE customers_id = <temporary id> 

If customers_id is a foreign key, you need to additionally create a temporary customer entry as soon as the first item is added to the shopping car. 如果customers_id 外键,则需要在第一个项目添加到购物车时另外创建临时客户条目。 Then, when the session expires, delete this entry, too. 然后,当会话到期时,也删除此条目。 And you need to delete the temporary entry, as soon as a registered user logs in, together with the update of customer_id in orderdetails. 并且您需要在注册用户登录后立即删除临时条目,并在orderdetails中更新customer_id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM