简体   繁体   English

如何将购物车数据的 session 信息添加到数据库中?

[英]How do i add information to a session of shopping cart data into a database?

i have a shopping cart and i wanted to store the shopping cart information to my database but i have no idea how to do it since i have no way to detect how many items my customer would add to their shopping cart.我有一个购物车,我想将购物车信息存储到我的数据库中,但我不知道该怎么做,因为我无法检测到我的客户将添加多少商品到他们的购物车中。

What i have in mind right now would be having a non-unique key as shopping cart id.我现在想到的是有一个非唯一的键作为购物车 ID。 IE IE

ID / book / price / quantity
1/harry potter/19.90/1
1/deadly hollows/49.90/1

and both of id 1 will consitute as a complete shopping cart.并且两个 id 1 都将构成一个完整的购物车。 Will there be something wrong with this way of doing?这种做法会不会有问题?

You should have a Primary Key that is unique, but can span multiple columns:您应该有一个唯一的主键,但可以跨越多个列:

You could probably use ID and book as your Primary Key, since each cart won't have 2 copies of the same book, because that would just update the quantity, not add a new row.您可能会使用 ID 和 book 作为主键,因为每个购物车不会有同一本书的 2 个副本,因为这只会更新数量,而不是添加新行。

A non-unique key is not a good solution.非唯一密钥不是一个好的解决方案。
I would add an auto-increment key to the shopping cart table and use that as "real" unique key.我会在购物车表中添加一个自动增量键,并将其用作“真正的”唯一键。
Just keep your non-unique ID as it is, so you can use it as an OrderID to see which rows belong to the same order.只需保持您的非唯一 ID 不变,这样您就可以将其用作 OrderID 来查看哪些行属于同一订单。

Like this:像这样:

UniqueID / OrderID / book / price / quantity
1/1/harry potter/19.90/1
2/1/deadly hollows/49.90/1
3/2/code complete/25.90/2

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

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