简体   繁体   中英

How to get the customer id when creating the customer and order at the same time?

I am trying to understand the database design for an e-commerce site. I am having trouble understanding on what to do in the following situation. Say the user creates an account for the first time, and makes an order. I can write php code which will add the user_id (primary-key), first name & last name. But what if I want to add the user_id in the orders table (user_id in the orders tables is a foreign key). How do I get the value, as the user_id in the customers table is auto incrementing?

您是否使用两个表来存储用户详细信息,另一个用于下订单?(更好)如果这样,我的建议是使用用户名(而不是名字或姓氏)作为主键,并将用户名存储为SESSION变量登录并为每个订单在订单表中添加用户名以及订单详细信息

有两种方法可以执行此操作,您可以通过LAST_INSERT_ID()访问新生成的user_id并在后续事务中使用它,也可以将其存储在会话中,稍后在插入Orders表时使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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