CREATE A PHP MYSQL SHOPPING CART
2
I am Robert Tou I am a web developer and share the love for programming by giving free presentations.
3
Everyday life is like programming, I guess. If you love something you can put beauty into it.
- Donald Knuth
1) THE DATABASE
» » » » »
Create a database to hold the products and orders first. Products – Product ID, Name, Description. Orders – Order ID, Name, Order Date. Ordered Products – Product ID, Order ID, Quantity. You can further expand on the database, but these should are the basic requirements.
4
2) PRODUCTS CATALOG
» Create a HTML page to pull and display the products. » Connect to the database, and use a simple “SELECT *
5
FROM `products`” SQL query to pull the data. » Use a while loop to generate a table, layout the products as you deem fit. » Remember to attach an “add to cart” button for each product.
3) CART ACTIONS I
6
» Recommended to use AJAX to handle the add to cart. » Use jQuery to speed up your development. » Pass the selected product ID via AJAX to another PHP script to handle the add action. » Start the session, and record the cart in a simple array of product ID => quantity.
4) CART ACTIONS II
7
» Handle the rest of the actions in a similar manner via AJAX. » Javascript fires an AJAX request to the PHP handler. » Change quantity and remove items from the session cart.
5) CHECKOUT
8
» Show the checkout button if there is at least 1 item in the cart. » Similar AJAX process. » Record cart into the database, wipe off the session, then redirect the customer to a thank you page.
Thank you!
For the complete guide and source code, please visit Code Boxx: https://code-boxx.com/simple-phpmysql-shopping-cart/
9
CREDITS
Special thanks to all the people who made and released these awesome resources for free: » Presentation template by SlidesCarnival » Photographs by Unsplash » Diverse device hand photos by Facebook Design Resources
10