1 of 7 2/17/2018, 7:46 AM Mukesh Chapagain Blog PHP Magento jquery SQL Wordpress Joomla Programming & Tutorial HOME ABOUT CONTACT ADVERTISE ARCHIVES CATEGORIES MAGENTO Home» PHP PHP: CRUD (Add, Edit, Delete, View) Application using OOP (Object Oriented Programming) 10 October 2016 About Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging. more... This article shows how to create a CRUD Subscribe via Email create database test; The connection has timed out (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. Email: Categories Categories Select Category Translate to your language Select Language Archives Archives Select Month 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
2 of 7 2/17/2018, 7:46 AM 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
3 of 7 2/17/2018, 7:46 AM 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.
4 of 7 2/17/2018, 7:46 AM 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: index.php
5 of 7 2/17/2018, 7:46 AM 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
6 of 7 2/17/2018, 7:46 AM 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
7 of 7 2/17/2018, 7:46 AM Download Full Source Code: Simple Create, Read, Update, Delete in PHP & MySQL using Object Oriented Programming (OOP) Hope this helps. Thanks. Share this: Related Posts: 1. CodeIgniter: Simple Add, Edit, Delete, View MVC CRUD Application 2. CodeIgniter: Add, Edit, Delete, View with Login & Register MVC CRUD Application 3. PHP MySQL: Simple CRUD (Add, Edit, Delete, View) using PDO 4. CRUD with Login & Register in PHP & MySQL (Add, Edit, Delete, View) 5. Very Simple Add, Edit, Delete, View (CRUD) in PHP & MySQL [Beginner Tutorial] 6. PHP & MongoDB: Very Simple Add, Edit, Delete, View (CRUD) [Beginner Tutorial] 7. Laravel: Simple CRUD (Add, Edit, Delete, View) [Beginner Tutorial] 8. Node.js, MySQL & Express: Simple Add, Edit, Delete, View (CRUD) 9. Node.js, MongoDB & Express: Simple Add, Edit, Delete, View (CRUD) 10. PHP: Google Spreadsheet with OAuth (Add, Edit, Delete, View data) Get New Post by Email Find me on