Introduction
Previously, we learned the basics from our PHP CRUD Tutorial for Beginners.
Today we welcome you to our PHP OOP CRUD Tutorial, a comprehensive guide on mastering PHP Object-Oriented Programming (OOP) for database management.
This tutorial will cover everything you need to know to create, read, update, delete, and search records in a MySQL database using Object-Oriented Programming (OOP) in PHP.
We will also show you how to upload files, making it an all-in-one resource for anyone looking to improve their OOP skills in PHP.
This tutorial will take you through the process step-by-step, providing you with a solid foundation in PHP OOP and database management. Let’s dive in!
- Database Table Structure
- Create a database
- Create products table
- Insert products sample data
- Create categories table
- Insert categories sample data
- Output
- Create the Layout Files
- Create header layout file
- Create the footer layout file
- Create a custom CSS file
- Output
- Creating record in PHP the OOP Way
- Create a file: create_product.php
- Create a “Read Products” Button
- Get a Database Connection
- Create the Database Configuration Class
- Create a Form in create_product.php
- Loop Through the Categories Records to show as Drop-down
- Create the Object Class for Categories
- Prepare readName() method
- Code when the Form was Submitted
- Create the Object Class for Products
- Output
- Reading and Paging Record in PHP the OOP Way
- Create File: index.php
- Add a “Create Product” button
- Configure Pagination Variables
- Retrieve Records from the Database
- Add readAll() Method in product.php
- Display data from the database
- Put the Read, Edit and Delete Action Buttons
- Create paging.php for Paging Buttons
- Add the countAll() method in objects/product.php
- Include paging.php in index.php
- Output
- Updating Record in PHP the OOP Way
- Create File: update_product.php
- Create a “Read Products” Button
- Retrieve One Product Information Based on the Given ID.
- Add readOne() method in the Product Object Class.
- Put the Values in the Form.
- Loop Through the Categories Records to show as Drop-down
- Code When Form was Submitted
- Update Code in the Product Class
- Output
- Read One Record in PHP the OOP Way
- Create read_one.php file
- Read one record based on given record ID
- Display record on HTML table
- Output
- Deleting Record in PHP the OOP Way
- Put this JavaScript code in layout_footer.php
- Create delete_product.php
- Delete Code in Product Class
- Output
- Search Records in PHP the OOP Way
- Change index.php
- Create read_template.php
- Create core.php in “config” folder
- Change paging.php code
- Include core.php and read_template.php
- Create search.php
- Add search() and countAll_BySearch() methods
- Output
- File Upload in PHP the OOP Way
- Change HTML form
- Set the value of the “image” field
- Change create() method
- Call uploadPhoto() method
- Add uploadPhoto() method
- Validate submitted file
- Return error messages
- Show the uploaded image file
- Output
- Download source codes
- Choose your download
- Why download the source codes?
- How To Run The Source Codes?
- What’s Next?
Database Table Structure
First, we’re going to create our database and tables. We will also put sample data in our database.
The files “products.sql” and “categories.sql” are also included in the code download, located in the “dev” folder.
Create a database
- Open your PhpMyAdmin (http://localhost/phpmyadmin)
- Create a new database.
- Put php_oop_crud_level_1 as the database name.
- Click the “Create” button.
Create products table
Next, we will create the “products” table on the database we just created. The products table will store the product data.
We will create the products table using a SQL statement. You can copy it below. Here’s how to run an SQL statement using PhpMyAdmin.
- Click php_oop_crud_level_1 database.
- Click the “SQL” tab.
- Copy the SQL statement below and paste it into the text area.
- Click the “Go” button.
Create categories table
The “categories” table is used to store product categories. Run the following SQL statement using your PhpMyAdmin.
Insert categories sample data
We are going to have “Fashion,” “Electronics,” and “Motors” as categories in our project. I got those three category ideas from eBay, haha! Run the following SQL statement using your PhpMyAdmin.
Output
In this section, we set up our database using PhpMyAdmin. It should look like the image below.
We don’t have a PHP program output yet. Let’s continue to the next section to achieve more output.
Create the Layout Files
The purpose of layout files is to have a reusable code for the header and footer of our application. This will make it easier for us to make our application look good.
Create header layout file
his “layout_header.php” file will be included at the beginning of the PHP files that will need it. This way, we won’t have to write the same header codes every time.
We use the Bootstrap framework to make our project look good. If you’re not yet familiar with you, please learn our Bootstrap tutorial here first.
Bootstrap CSS asset will be included inside the head tags.
- Create the “PhpOopCrudLevel1” folder and open it.
- Create layout_header.php file.
- Place the following code.
We don’t have a PHP program output yet. Let’s continue to the next section to achieve more output.


