8 Simple Steps to Create CRUD Application in OOP PHP

8 Simple Steps to Create CRUD Application in OOP PHP

This is simple CRUD Application in OOP PHP (Object Oriented PHP). In this tutorial, you will learn about CRUD operations in OOP PHP.

CRUD stands for create, read, update and delete. Create means inserting data into the database using INSERT SQL statement. Read means reading data from database using SELECT SQL statement. Update means updating records using UPDATE SQL query. Finally, Delete means deleting data from database using DELETE SQL statements.

Here I’m going to create five PHP files, each PHP file is for create, read, update, delete functions and the other file is for Database class for OOP operations. And I’m creating a table with simple columns first_name, last_name, gender, age, email_id.

Using these five files, I’ll insert data into database (C), Reading data from database (R), Updating data in database (U), Deleting data from database (D).

And I’ll name these files in simple terminology

  • C – Create : index.php – To insert data into database (INSERT SQL Query).
  • R – Read : view.php – To read data from database (SELECT SQL Query).
  • U – Update : upate.php – To update data in database (UPDATE SQL Query).
  • D – Delete : delete.php – To delete data in database (DELETE SQL Query).

This is the continuation from the Simple CRUD Application in PHP & MySQL. I’m reusing the same code for creating CRUP Application in OOP PHP.

1. Creating Database Table

2. Creating Database Class

For OOP CRUD operations, I’m creating Database class with this database class I’ll do all the CRUD operations.

First I’m creating a database class and instantiating the object with the new database. After that whenever we include this file in other PHP files that is index.php, view.php, update.php, delete.php we can use database object. We will see how we can use this database object in next steps.

Here is the code to create database class and instantiate the database object.

Save this file as database.php

2. Connecting to Database Table using connect.php file

For connecting to the database, I’ll create a new method with the name of connect_db, and when we want to connect to the database we should call this method. Also, we will see later in this step, how we can connect to the database with a constructor.

I’ll call this method right after instantiating the object.

Here is the code to connect_db method and connecting to the database by loading connect_db method.

We can connect to database after loading database.php file in other files like index.php with constructor.

Here is the code for the constructor.

3. Create index.php HTML Form

Here I’ve created a simple HTML form with few input fields to insert data into database and also we will use this same form in update.php.

This form contains 5 input fields with the first name, last name, email, gender, age.

Here is the code of HTML form, save it as index.php

4. INSERT data into database

First include database class file that is databse.php file in index.php file using require_once PHP function.

Then check POST superglobal using if condition and isset, empty PHP functions. Then assign form values to variables, while assigning these values to variables use mysqli_real_escape_string function to strip data from input fields.

We can do one more thing that is we can create a method to sanitize the user submitted data by passing this information and returning the sanitized data.

Add this code in database.php

Here is the code to sanitize user-submitted data after form submission and assigning returned values to variables.

Then, after assigning these values into variables, We will call a method to insert this data into the database.

Next, I’ll create a method for create operation with insert SQL query.

Add this method to database.php file.

Then we can load the method by passing the sanitized data. We will get true or false based on the query.

If it’s true we will display success message or else we will display failure message.

After adding above code, if you submit the form. A record should be inserted into the database.

5. Create view.php HTML File

Next is read operation, in this view.php file, I’ll display all the records fetched from database in nice HTML table format.

Here is the code for HTML table, save it as view.php

6. Fetch data from datbase in view.php file

Include database class file database.php in view.php file for instantiating to database class.

Next, to read data from the database, we should create a method with SELECT SQL Query. Here I’m selecting all the rows from the database using table name. And returning the result set. With this result set in view.php we will use while loop to loop through the results.

Here is the code for read method in database.php

Add this code to view.php file

And also use this code in second tr block

After adding the above code, if you reload the view.php file. You can see all the records from database table.

7. Create update.php HTML file

For update.php file, I’m going to use same HTML code as index.php file. Because, in UPDATE operation, we will get the data from the database for specific row based on id. Then the user will edit the values and submits the form after that script will update the values in the database.

In previous file view.php, I’ve already created links for edit.php & delete.php files also passing id values from the database. If anyone clicks on the links, these id’s will be passed on to next file that is edit.php or delete.php files.

Here is the HTML code for update.php file

Here in update.php file, we will use two methods that are reading and updating. In previous read method, we are getting all the records. Now, we want to get only one record if that I’ll add an extra bit of code to read method without creating extra method.

First of all, we should load database.php file in update.php to load database class. After that, I’m id value passed through URL to id variable.

Then loading read method with the id variable. Fetching results with the myqli_fetch_assoc PHP function.

Here is the form with the value attributes

Next, we should update the record after form submission for that I’ll create a new method with update SQL query.

Then after form submission, we should load this update method. Before that, we should check the form is submitted or not, by writing an if condition isset post and not empty.

And sanitizing the user submitted data with sanitize method.

Next, we will load the update method with user submitted data. If the result is true we will display success message or else we will display failure message.

8. Delete records from database using delete.php file

Next, after clicking delete button user will be redirected to delete.php file with id in URL. We will delete the record in delete.php with the help of delete method.

If the record is successfully deleted then we will redirect the user to view.php file. Otherwise, we will display an error message to the user.

First of all, I’ll create delete method with delete SQL query. If the query is successful we will return true or else we will return false.

In delete.php file, I’m loading database class file that is database.php. Just include this file using require_once PHP function. Next is assigning the id passed through URL to id variable.

After that, I’m loading the delete method by passing id variable assigning it to result. If the delete query is successful I’m redirecting the user to view.php and it’s unsuccessful I’m displaying failure message.

Till now, we have successfully created CRUD application in OOP PHP. If you have any issues let me know through comment form below.

Complete Code of All Files

If you have any problem arranging above pieces of code, you can use this below complete code.

If you are getting any issues with above code, let me know through the comment form below.

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 5 comments

Great code simple and easy to follow

Viku chandrakar

Txs sir…

Abraham M Sheriff

Indeed,the code is easy to follow thanks a lot for your service. But I am still encountering some problems with database connectivity, I am done with database.php and index.php but when I try to input data to the database through the form in the browser it can’t take effect. And i have created database with table. I have also included database.php file in index.php along with some methods and functions but still is not inserting data.


    Vivek Vengala

    Hi Abraham,

    Can you post the error message you are getting?

    I encourage you to download the complete OOP CRUD application…


Leave a Reply: