This guide covers PHP MySQL CRUD OOP with practical steps you can copy into a real project.
PHP MySQL CRUD OOP
CRUD stands for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for a database. Create means insert data using an INSERT query. Read means read data using a SELECT query. Update means edit data using an UPDATE query. Delete means remove data using a DELETE query.
First, we create a database in MySQL.
Create database crud;
Now create a table in the crud database.
Crud.php is the main file. In this file we will include the database connection plus insert, read, update, and delete.
First we create a database connection class named database. This class contains the connection code. Here we define the database host, database name, username, and password.
Crud.php
Create an insert.php file for the HTML form.
To insert employee data into the table we need an HTML form. This form has seven fields: first name, last name, gender, email, birthdate, department name, and profile picture.
Insert.php
Now insert data into the database.
First we include the database class file in insert.php using the include function.
Include crud.php;
Then check whether the submit button was set. Assign the form values to variables using mysqli_real_escape_string to sanitize the input fields.
Include this code in the crud.php file.
After the employee submits the form, this data is stored in PHP variables.
This data is stored in the database using an insert query. We will create an insert method with that query in crud.php.
This function returns true or false. If it returns true we print a success message; if it returns false we print a failure message.
Display all employee data in index.php
We will fetch data from the database and display it in index.php. This file is the home page. It will link to adding data. On every row we also give links to edit and delete that record.
index.php file
Fetch data from the database
First include the database class from crud.php in index.php using the include function.
Include crud.php;
Now all employee data is fetched from the database using a select query. We will write a read method with that query in crud.php.
Now this function is called from index.php. Add the code below in index.php.
$result = $database->read();
Using a select query we select all data from the registration table. We use mysqli_fetch_array() to fetch rows from the database.
Add this code in index.php.
Update employee data
First we create an HTML form to update data. Each row can be updated separately. The employee ID is passed in the URL of update.php.
In the code below, each row is fetched based on the ID. The fetched data is shown in the update form. We use the read function to select data from the database.
HTML form for update
Now we create an update function with an update query in crud.php.
After form submission, we first check whether the form was submitted by testing if update is set.
Add this code in the update.php file.
Delete employee data using delete.php file
We delete employee data with a delete query. In index.php, clicking the delete link redirects the user to delete.php with the employee ID in the URL.
In delete.php we include the database class from crud.php. Then we assign the ID from the URL and call the delete method with that ID.
Now we create the delete method in crud.php. If the query succeeds it returns true; otherwise it returns false. If it returns true we redirect the user to index.php.
Have a look, and feel free to comment if you have any questions.
Related reading
Questions? Use the contact page.
Reference: PHP OOP docs.


Great article and examples! It saved a lot of time. It is just what I needed.
03/08/2020 at 10:20 amWe are glad to be of help.
10/08/2020 at 8:54 amGreat artical for Developer so Beginners are learning More From Your artical
Great Work Borther
21/06/2022 at 6:23 am