Building An E-commerce Site From Scratch [Part 3]
In this part of the project, i talk about making a CMS and building the last 3 of the 4 basic operations of data persistence: Read, Update and Delete.
![Building An E-commerce Site From Scratch [Part 3]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Funsplash%2FnpxXWgQ33ZQ%2Fupload%2Fv1646048546433%2FwMF3S1Pij--.jpeg&w=3840&q=75)
I build things that scale, perform, and make sense. As a fullstack developer with a deep obsession for clean architecture and high-impact systems, I use code as both a creative and strategic tool. My work spans backend logic, frontend clarity, and the glue between always with a bias toward simplicity, performance, and maintainability. I write to distill complexity, document hard-earned lessons, and share patterns that save time, reduce friction, and unlock better thinking. No fluff, no filler, just honest takes. This blog is where I think out loud. If you build stuff too, we’ll probably get along.
We previously talked about building a content management system (CMS), and the four basic operations of data persistence. We’ve already covered create, so we’ll look into the other three: read, update and delete.
Read
When any data is created, they are pretty much useless if they arent read or used. Just use a book for example. If an author writes a book and locks it away from the public or others then that book is useless, and all effort that went into writing it was pretty much in vain.
After a product is created it needs to be shown to both the end user and we the admin. It is important we the admin are able to see our data inputs for obvious reasons.
On the admin side/folder of the site I created a page called products.php. From this page we can handle everything related to products. On the functions.php page a function is created to query the database for all products available, then renders the result in a table. I also use a tool called dataTable to better layout the data. dataTable also contains functions to aid in searching and pagination. It is a tool really worth looking into. The data will still be read into the front-end after the admin is built. Later we’ll see the importance of extensively laying out our database in sync with our template. It should be kept in mind that the smaller number of times a database is queried, the faster and healthier your app or site becomes. I try as much as possible to get all data in one query then laying them out.
Update
After we're done reading all our data into a table in a way that we can easily see all items we’ve created without going directly to the database management system, in my case phpMyAdmin, then we can move forward to the next operation.
Update is exactly as it sounds, whenever data is created, mostly due to human error and time passing, it can get erroneous and stale. In order to combat this, data needs to be reviewed or cleaned every now and then. Keep in mind though that this can be optional, depending on the nature of what you’re building. Sometimes data needn’t be updated but rather deleted and recreated. A good example of this is Twitter where tweets can’t be updated for the purpose of accountability.
An e-commerce site certainly needs update functionality because items will most likely be changed sooner or later. The importance of unique identifiers is greatly emphasized here. These are the summarized practices I used:
- Inside our products table I created a column for updating. Inside the column is a font awesome icon for editing(fa-edit) (google font-awesome if you haven’t used it before) wrapped with a dynamically created link.
- The dynamic link is created by getting information from the particular row about to be edited and passing them in as PHP GET values. A particular row is known by their unique id’s. The link should look like this: ecomstore/admin/newproduct.php?id=16&title=apple-iphone6&edit=1.
- When the link is clicked the old info is filled into their corresponding fields so the user doesn’t have to start over.
- When the user submits the form, through the link it knows this is an update(”...&edit=1”). Thus the right function to update the product is run.
I’m using MySQL so the code should look something like this:
If the above process is a bit difficult to understand, just google unique identifiers in databases and how they are used to target specific rows."UPDATE `item` SET `title` = 'Apple iPhone 6' WHERE `item`.`id` = 16
Delete
In the words of a famous philosopher Kamahi:
Destruction is the work of an afternoon; Creation is the work of a lifetime.
Deleting a row in a table or destroying a table is scarily simple. All it takes is one command. This is where we have to be very careful, most especially if our site or project is live.
As dangerous and destructive a delete query is, it is very important in keeping our database healthy. As our immune system works in killing germs and bacteria and keeping our bodies healthy, deletion helps us remove impurities or unwanted data in our database.
Just as an update column is added in our products table, another for the delete function will be created as well. And they both work the same way: using a dynamically created link to target a specific product or data row and performing a task on it. If your project will have a lot of admins, it is best to grant delete access to a select few or super admins.
Summary
The four basic operations of data persistence are all essential to all and any database. There are other operations but these are the most basic ones. All progress has been committed to [Github] as at the time of writing. Stay tuned for part 4, I’ll talk about Registration, Login and Authentication. If you have any questions or contributions please drop a comment.
![Building An E-commerce Site From Scratch [Part 4]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Funsplash%2F6pflEeSzGUo%2Fupload%2Fv1646601526445%2FOrOczOXX5.jpeg&w=3840&q=75)
![Building An E-commerce Site From Scratch [Part 2]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Funsplash%2FhpjSkU2UYSU%2Fupload%2Fv1645898227301%2F9VUtvUn5z.jpeg&w=3840&q=75)
![Building An E-commerce Site From Scratch [Part 1]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Funsplash%2FHcfwew744z4%2Fupload%2Fv1645891908785%2FHzUbff41S.jpeg&w=3840&q=75)