PHP如何识别购物车中用于REST API的用户项目 [英] PHP how to identify user items in cart for rest api
问题描述
我正在用php为电子商务应用程序创建基于令牌的rest api.
场景: 任何访问者都可以在不登录的情况下将商品添加到购物车.这些商品存储在mysql数据库的购物车表中,其user_id值默认为1.
问题: 用户登录后,我可以在解码为用户生成的令牌后获取用户ID,但想知道,我如何识别购物车表中的哪些项目属于哪个用户,以便针对这些产品更新实际的用户ID. ?
表格:
customer_id int(11),
item_id int(11),
数量 int(11),
添加了日期的 日期时间
在此先感谢您的帮助!
您应生成(临时)标识.我会向您的数据库id
添加一个自动递增的列.创建数据库后,您可以使用诸如PDO mysqli_insert_id($conn)
之类的内容检索ID.将该ID存储在PHP会话中,就可以了!
I'm creating a token based rest api in php for e-commerce application.
Scenario : Any visitor can add items to cart without logging in. These items are stored in the mysql database, cart table, with user_id value which defaults to 1. // As user is not logged in.
Problem : After the user logs in, i am able to fetch the userid after decoding the token generated for the user, but want to know, how can i identify which items in cart table belongs to which user so as to update the actual userid against those products ?
Table :
customer_id int(11),
item_id int(11),
quantity int(11),
date_added datetime
Thanks in advance for any help!
You should generate a (temporary) identification. I'd add an auto incremented column to your database id
. After you have created the database you can retrieve the id with something like PDO mysqli_insert_id($conn)
. Store that id in a PHP session and you have it!
这篇关于PHP如何识别购物车中用于REST API的用户项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!