How to Create Simple CRUD Application in PHP PDO - 7 Easy steps
  • Home
  • PHP

How to Create Simple CRUD Application in PHP PDO – 7 Easy steps

Develop Secure Applications with PHP PDO – Ultimate Guide

Using PHP PDO is recommended because it’s more secure and protects from SQL injections. PHP PDO CRUD application, previously we have seen CRUD application in PHP & MySQL. Here in this article, I’m going to build CRUD application with PHP Data Objects.

Before continuing with creating CRUD application in PDO, you need to know basics about PDO. If you don’t know anything about PDO, don’t worry I’m creating a course for you to easily understand PHP PDO, It will out soon. I’ll update you as soon as it’s published.

This article is same as the PHP CRUD application, here I’m using PDO style. All the features are same.

Other Articles in Learn PHP PDO

About CRUD Application with PHP PDO

Coming to CRUD application, it’s simple CRUD application with few input fields. With this application we will insert submitted form data into the database, this is the create operation.

Next is displaying the records that are fetched from the database, we will display this data in table format. This is the Read Operation.

Next is Update operation, we will fetch the data from the database and displayed in the form instead of a table. And we will update this data in the database after submitting using id, this specific record will be updated using id passed in the URL. This is Update Operation.

And final operation is delete operation. After clicking on the delete link from Read Operation page record will be deleted from the database.

1. Create Database Table

First of all, we need to create the database. If you are following from the PHP CRUD application you can use the same database. Here is the SQL code to create the database table.

2. Creating Simple HTML Form

Here I’ve created a simple bootstrap form with these fields. Firstname, lastname, email, age, gender. It’s simple form HTML.

3. Connecting to Database with PHP PDO

In PHP PDO, we can connect to the database by creating new PDO object and we have to pass database type, hostname, database name, database username, database password.

Here I’ve created a DSN variable with database type, here I’m using the mysql database. And the hostname is localhost, use your hostname here. Next value is database name.

I’m creating database object by instantiating PDO with dsn variable and database username, password.

Here is the code you can use. Save it as the connect.php file.

4. Inserting Records with PHP PDO

Before going on to insert the record, we should include connect.php file in insert operations PHP file. Here I’m going to insert the record from index.php file. Below is the code to include the file.

After that we should catch the errors for that I’ll use a try-catch block. We can catch the errors in two ways that is using setAttribute method and with the errorInfo method.

Here I’m using the setAttribute method. Inside try block I’m using require_once PHP function to load connect.php file and after that setting error mode with the setAttribute method.

In catch method, I’m assigning the exception to $e, then catching the errors with exception getMessage method.

Here is the code

To display error messages use this code

I’ll use the above form with bootstrap styles.

After form submission, I’m checking post superglobal is set and not empty with if condition. Here is the code

Here I’m using prepared statements to insert submitted data into the database with named placeholders. There are other methods like named array placeholders.

Here is the SQL query with named placeholders

After that we will prepare the statements, Then we will execute it with values linking with named placeholders. Until execute method, records won’t be inserted in the database.

Here is the code to prepare and execute methods.

After that, I’ll check whether $res is true or false. If it’s true we will display success message or else we will display an error message.

5. Fetching Records with PHP PDO

This is Read operation, Here I’m fetching records with query method and displaying these records in the table. And the code of the HTML table, you can find below.

Use this table HTML code and save the file as view.php

We will display the fetched data in the second tr with a while loop.

First of all, we will see SELECT SQL query with query method. Here is the code.

For connecting to the database and also for catching errors I’m using the same code as above.

To get records from the result I’m using PDO fetch method. PDO fetch method returns two types of records that are numeric array and the associative array. To get only associative array I’m using PDO style inside PDO fetch method.

And the code looks like this…

PDO fetch method returns only one result, to get multiple records with fetch method we should use while loop. And the code for PDO fetch method with while loop is

Now we can echo the information fetched from the database.

6. Updating Records with PHP PDO

In update operation, we have to perform two operations that are Read operation and Update operation.

And the file name is update.php

Read operation is for fetching data from the database and displaying it in form. After that when users submit the form, this information will be updated in the database associated with that id.

In the view.php file, we are fetching all the records available in the database table. Here we will fetch only 1 record with that id. Here I’m using prepared statements for select query also because we are sending id from URL that is with get method.

Here I’m using anonymous placeholders to pass the id. Because we are just passing only one value that is id.

Here is the code for SQL query, prepare statement and execute method

Here in update.php file, I’m using the same form as in index.php and the only difference is here I’m passing the values in the form. To display the values in the form.

Displaying information in the form is completed, next thing is updating the record after form submission.

This update operation is same as the insert operation and the only difference is here we will use only update SQL query instead of insert SQL query everything is similar.

You can go through the code the only difference is SQL query.

You can display success or failure message same as the index page. Here is the code

7. Deleting Records with PHP PDO

Delete Operation is same as it suggests, it deletes the record from the database table. We will pass the id in the URL, record will be deleted. Here also for the delete operation, I’ll use prepared statements because id is passed through the URL.

This delete query is also same as the select query in update.php file. Instead of select SQL query, here I’ll be using delete SQL query with anonymous placeholders.

And the final code of delete.php file is

If the delete query is successful, we will redirect the user to view.php or else we will display an error message. Here is the code to redirect and display an error message.

Complete Source Code of PHP PDO CRUD Application

In case if you have any problem with arranging above pieces of code, you can use this complete code.








If you are not using PHP PDO in your applications, it is recommended to use PDO in your applications. To improve security and also not hacked by SQL injection attacks.

If you want to learn more about PHP PDO, you can join my course. It will be published soon.

If you have any doubts, let me know through the comment form below.

Vivek Vengala

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