简体   繁体   English

带有会话和数据库的php购物车

[英]shopping cart in php with session & database

I had developed a shopping cart in PHP using sessions and a database. 我使用会话和数据库在PHP中开发了一个购物车。

If the user is not logged in (a guest), the cart items are stored in the $_SESSION["cart"] variable. 如果用户未登录(访客),则购物车项目存储在$_SESSION["cart"]变量中。

If the user is logged in, all of the cart items from the $_SESSION["cart"] (if any) are copied to database and any previous cart items from database (if any) are also copied back to $_SESSION["cart"] . 如果用户已登录,则将$_SESSION["cart"] (如果有)中的所有购物车项目都复制到数据库中,并且还将数据库中的所有先前的购物车(如果有)都复制回到$_SESSION["cart"] Here I'll keep the copies both in the session and the database. 在这里,我将把副本都保留在会话和数据库中。

As long as user is logged in i don't have the problem with the cart items. 只要用户登录,我就不会遇到购物车项目的问题。

Since I am using session for guests, I'll certainly lose the cart details as soon as browser is closed. 由于我正在为访客使用会话,因此,我肯定会在关闭浏览器后丢失购物车详细信息。

How can I overcome this scenario? 如何克服这种情况?

I am trying to store guest cart details in a database, with a unique cookie value. 我正在尝试使用唯一的cookie值将访客购物车详细信息存储在数据库中。 Suggestions please? 有什么建议吗? cart Table Details : 购物车表详细信息:

cart_id*
user_id
product_id
product_option_id
product_count
total_amount
time_stamp

user_id+product_id+product_option_id is Unique

Here is the program flow that I would make. 这是我要制作的程序流程。 It's vague so you can implement it however you want to. 它含糊不清,因此您可以根据需要实施它。

  1. Create a random session ID, perhaps even as simple as with Math.random() . 创建一个随机会话ID,甚至可能与使用Math.random()一样简单。
  2. Check in DB of carts to see if that ID is taken. 检入购物车数据库以查看是否已使用该ID。 If so, go back to step 1. 如果是这样,请返回步骤1。
  3. Create an entry in carts DB with that session ID, with the cart ID. 在具有该会话ID和购物车ID的购物车DB中创建一个条目。
  4. Create a cookie $_COOKIE["carts"] with that session ID. 使用该会话ID创建一个cookie $_COOKIE["carts"]
  5. Every time the user updates the cart (ie, adds or removes an item), check the cookie for the session ID and change the info from the DB. 每次用户更新购物车(即添加或删除商品)时,请检查cookie以获取会话ID并从数据库中更改信息。
  6. When cart is submitted, retrieve session ID from cookie and retrieve DB info with that session ID, and then do your PHP magic to buy (empty the cart) with that data from the DB. 提交购物车后,请从cookie中检索会话ID,并使用该会话ID检索数据库信息,然后用PHP的魔力从数据库中购买(清空购物车)该数据。
  7. Remove the cookie and delete the entry from the database. 删除cookie并从数据库中删除条目。

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

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