Home » PHP10 October 2016

PHP: CRUD (Add, Edit, Delete, View) Application using OOP (Object Oriented Programming)

This article shows how to create a CRUD (Create, Read, Update, Delete) application system with PHP & MySQL using Object Oriented Programming (OOP) technique. I had written an article before about creating Simple CRUD application with PHP & MySQL but that has been programmed with procedural way. In this article, we create the same kind of CRUD application using OOP.

First of all, we will create a new MySQL database. Let us name the database as ‘test‘.

create database test;

Then, we will create a new table in database ‘test’. Let us name the table as ‘users‘.

use test;

CREATE TABLE users (
id int(11) NOT NULL auto_increment,
name varchar(100) NOT NULL,
age int(3) NOT NULL,
email varchar(100) NOT NULL,
PRIMARY KEY (id)
);

Now, we will create a database connection class named DbConfig. We put our classes inside a folder named classes. This classes folder will be inside our website root folder. So, the path of DbConfig class will be classes/DbConfig.php. This class contains database connection code. Here, we define our database host, database name, database username and database password.

classes/DbConfig.php

There are other two classes for this example application. One is the Crud class and the other is Validation class. Crud class is responsible for executing database queries. Validation class is responsible for checking correctness of form POST data, like checking for valid email, valid age, etc.

classes/Crud.php

classes/Validation.php

To add data into database, we need an html form.

add.html

Form action on add.html is add.php. It means that the submitted form data will go to add.php. In add.php, we do a simple validation of checking if the entered name, email & age are empty or not. If they are all filled then the data will be inserted into database table.

The validation part is handled by a new class named Validation. Functions like check_empty, is_age_valid, is_email_valid are present in the Validation class with specific purposes.

As you can see in the below code, we first instantiate the Crud class for database connection and then we instantiate the Validation class. We then check the validation of POST data. If everything is fine then we run the execute() function of Crud class.

add.php

Data from database is fetched and displayed in index.php file. This is our homepage. This file also contains a link to add data. On every row of displayed data, there is also a link to edit and delete data. Below is a sample image of our homepage:

CRUD PHP MySQL

index.php

Each row of data can be edited separately. Row ID is passed in the URL of edit.php. ID uniquely identifies the data entry.

While adding data, we had two files: add.html and add.php. While editing data, I have kept the entire thing in a single edit.php file. Edit form in HTML and database update code in PHP are present in the same file.

In the code below, at first a single row entry of data is fetched based on the id. The fetched data is displayed in the edit form.

edit.php

The edit form action is editaction.php. When the edit form is submitted, it goes to editaction.php. Here, some validation is checked for the POST data and then the particular database table row is updated.

editaction.php

Each row of data can be deleted separately. Row ID is passed in the URL of delete.php. ID uniquely identifies the data entry. After deletion, the user is redirected to homepage (index.php).

delete.php

Download Full Source Code: Simple Create, Read, Update, Delete in PHP & MySQL using Object Oriented Programming (OOP)

Hope this helps. Thanks.

PHP

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • topcat

    $result = $crud->getData(“SELECT * FROM users WHERE id=$id”);

    any input param to SELECT should always be escaped with msql_real_escape_string otherwise this is vulnerable to sql injection.

  • You are right. Thanks for the correction. I have added real_escape_string while adding and editing data.

  • topcat

    $result = $crud->getData(“SELECT * FROM users WHERE id=$id”);

    edit.php line 11 still remains :)

  • topcat

    Same for delete.php, crud->delete

  • Fixed them. Thanks :)