CRUD Operation Using OOP in PHP and MYSQL - Z Index Solutions
CRUD Operation Using OOP in PHP and MYSQL


CRUD stands for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for the database. Create means insert data in the database using INSERT Query. Read means read data from the database using the SELECT Query. Update means edit data in the database using UPDATE Query. And Delete means delete data from the database using DELETE Query.

First, we create a database in MYSQL.

Now, Create a table in crud database

Crud.php is the main file.  In this file, we will include data connection, insert, read, update, delete.

In this file first, we will create a database connection class named database. This class contains the database connection code. Here, we define our database host, database name, username and database password.


Create insert.php file for HTML Form

To insert employee’s data in the database table we need to create an Html form. This form contains 7 input fields of employees First name, employees Last name, employees Gender, employees Email, employees Birthdate, department name, and profile picture.


Now, Insert data into database

First, we include the database class file in the insert.php file using include function.

Then check if condition isset submit. Then assigning the form value in variables using mysqli_real_escape_string function to strip data from input fields.

Include this code in crud.php file

Then employees submitted data then after this data store in PHP variables.

Now, This data is stored in a database using the insert query. So, we will create an insert method  with insert query in crud.php file

In this function return true or false. If return true we print a success message and return false then we print a fail message.

Display all employees data in index.php file

We will fetch data from the database and display it in the index.php file. This file is our home page. This file will give the link of additional data. And every row of display data, we also give a link of edit data and delete data from the database.

Index.php file

Fetch data from the database

First, include database class from crud.php file in index.php file using include function.

Now, all employees data fetch from the database using a select query. So, we will write a read method with the select query in the crud.php file.

Now, this function call from the index.php file. So, add the below code in index.php.

Using select query we select all data from the register table. We use mysqli_fetch_array() function for fetch data from database.

Add this code in the index.php file.

Update employee data

First, we create an Html form for update data. Each row of data can be updated separately. Employee Id is passed in the url of update.php.

In the below code, each row entry of data is fetched based on the id. The fetched data is displayed in the update form. We use read function to select data from the database and fetch result.

Html form for update

Now, we create an update function with an update query in the crud.php file.

Then after form submission, Before we will check the form is submitted or not, using if condition check update isset or not.

This code add in the update.php file.

Delete employee data using delete.php file

We delete employee data using the delete query. In the index.php file, we click on the delete link then user redirect in delete.php file with employee id in URL which one has deleted a record.

In the delete.php file, we include database class file is crud.php using include function. Then assign id which is passed in URL. Then we will call the delete method from delete.php file with id.

Now, we will create the delete method in crud.php file using the delete method. If a query is a success then return true else it return false. If return true then we will redirect the user in the index.php file.

Just check and fill free to post comment if have a any query.

2 responses to “CRUD Operation Using OOP in PHP and MYSQL”

  1. SEO Company says:

    Awesome post! Keep up the great work! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *



Jn. Developer

Bhakti work as a web developer at Z Index Solutions.