Feb 20, 2020

PHP CRUD Operation With Search Query

Updated: Jul 28, 2021

What is CRUD?

CRUD is an composition of four words i.e., Create, Read, Update, and Delete. CRUD operations are basic data manipulation for database. Here we will create a simple PHP application to perform all of these operations on a MySQL database table at one place.

Well, now begin with developing a table which we're going to use in all of our example.

CRUD Operation

Creating the Database Table

At first we have to create a table, execute the following SQL query to create a table named student inside MySQL database. We will use this table for all of our future operations.

SQL Query

Creating the Config File

After creating the table, we need to create a PHP script in order to connect to the MySQL database server. Create a file and name it "config.php" and put the following code inside it.

Connection Code

We will later include this config file in other pages using the PHP require_once() function.

Creating the Platform Page

Now we will create a platform page for our CRUD application that contains a data framework showing the records from the student database table. It also has action icons for each record displayed in the table, that you can choose to view its details, update it, or delete it.

We will also add a create button on the top of the data table that can be used for creating new records in the student table. Create a file and name it as "index.php" and put the following code in it:

index.php

Once student table is loaded with some records the platform page i.e. the CRUD data table may look something like the picture shown below:

Platform Page

Creating the Create Page

In this section we will build the Create functionality of our CRUD application.

Create a file and name it as "create.php" and put the following code inside it. It will generate a web form that can be used to insert records in the student table.

create.php

The same "create.php" file will display the HTML form and process the submitted form data. It will also perform basic validation on user inputs before saving the data.

Create New Record

Creating the Read Page

Now it's time to build the Read functionality of our CRUD application.

Let's create a file named "read.php" and put the following code inside it.

read.php

It will simply retrieve the records from the student table based the id attribute of the student.

Read Record

Creating the Update Page

Similarly, we can build the Update functionality of our CRUD application.

Let's create a file and name it as "update.php" and put the following code inside it.

update.php

It will update the existing records in the student table based the id attribute of the student.

Update Record

Creating the Delete Page

Finally, we will build the Delete functionality of our CRUD application.

Let's create a file and name it as "delete.php" and put the following code inside it.

delete.php

It will delete the existing records from the student table based the id attribute of the student.

Delete Record

Creating the Error Page

At the end, let's create one more file "error.php". This page will be displayed if request is invalid i.e. if id parameter is missing from the URL query string or it is not valid.

Creating the Search Page

Now, we will build the Search Page which will allow us to find any data present in our database. Create a file and name it as "search.php" and put the following code inside it.

search.php

It will search the existing records in the student table based the name attribute of the student.

Result Found

If no result is found it will display a error message

Not Found

After a long journey finally we've finished our CRUD application with PHP and MySQL. and the Search Query on the database.

Want to download code? GitHub Link. Click Here. If you like Codersarts blog and looking for Assignment help, Project help, Programming tutors help and suggestion you can send mail at contact@codersarts.com.

Please write your suggestion in comment section below if you find anything incorrect in this blog post.