I am making a shopping cart for my website.
Which is better 开发者_如何转开发solution, storing data in a session or storing data in a table? If I store it in table how and when will I delete the records in the table (i.e when not purchased by user but in table)
What I do in my specific case is store the info in a CartOrder table. The status is 'pending'. When the process is finished I insert the info in the Order table and flag the CartOrder as done (or delete it). For me a CartOrder is the intention of a purchase, the Order is the purchase per-se.
Doesn't matter if you don't delete the CartOrder, you can use it later (take a look how amazon or other sites maintain your last cart available even if you didn't finish the purchase). This also would be nice to track how many users enter into the site, look around, start a cart but no finish the purchase. Users loves this.
Matter of preference. Both are reasonable. DB provides you the ability to analyze what was about to be purchased or at least queued up in terms of interest in your customers, that data is often quite valid as marking keen interest in your offerings and many times that data is NOT deleted, just has an expiration date or at least a session id as an attribute in the table.
As per the DB method, if you do want to delete it is an easy purge that you can run daily or weekly in a cron or other scheduled job to delete all records that have effectively expired.
depend what you want to do.
The usal solution use the $_SESSION
, this solution is easy to implement and won't hit the performance of your website.
The database session is nice because there is no timeout involved and it a bit more reliable.
I would say if your price tags are kind of expensive it's better to have a DB session because the user usually spend more time to think when he is spending > $10,000.
For normal products the SESSION solution is fine.
Take a hint from Amazon, store the data in a database and never delete it.
How much value to you get from the items in the cart? How much does it cost you to store the contents of the cart? If someone abandons their cart, and then comes back to your site a year later, they may still want the items in the cart.
精彩评论