Download presentation
Presentation is loading. Please wait.
Published byLeonard Hines Modified over 8 years ago
1
CSCI 6962: Server-side Design and Programming Shopping Carts and Databases
2
Outline Cart IDs Creating a customer table Structure of the carts table
3
Storing Shopping Carts Goal: Store cart in database between user sessions – If first user visit, cart empty – If user has created cart previously, retrieve it One solution: – Assign each cart a unique cart ID – Associate cart ID with key user field (such as email) – Store all cart items in single table – Each record contains corresponding cart ID
4
Customer Table Key field (such as email address) Cart ID of current cart Perhaps other customer information (address, etc.)
5
Signing In User prompted to “sign in” with key field – Will probably also ask for password (separate table) Query customer table for the key field If not found, generate new cart ID – Should not be session ID (too public) – Can be random number – Query database to make sure not already used Insert new record with Key field and cart ID
6
Carts Database Big table containing all items for all customers – Cart ID of the cart item belongs to – Item ID of that item – Other information that cannot be recreated from the item ID (such as quantity)
7
Loading a Cart Query Customer table for key field on user sign in If user record found, extract corresponding cart ID Query Carts table for all records with that cart ID Loop through results: – Extract the Item ID from that record – Extract other information (such as quantity) – Use ID to construct an item object – Add that object to the Cart object
8
Storing a Cart Key: When Cart ID generated/retrieved, store in session For each cart item insert record into Carts table: – Cart ID (from session) – Item ID (from cart item) – Other necessary data (quantity, etc.)
9
Storing a Cart Problem: That user may already have a cart stored in the table – Simply adding the current cart would create duplicate records for items saved previously Solution: – Delete all records from Carts table with the same Cart ID (removing previous cart) – Store all items in current cart as new records Make sure both done as single transaction
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.