top of page

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.

Display data in database through CRUD Operation
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 to create a table in database
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.

This the config.php file which is responsible for connection
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:

This is the index page
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:

This is the index page and we can perform all the operations from here
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.

Through this we can update any choosen record
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.

We can delete the choosen record
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.

Found result through search query
Result Found

If no result is found it will display a error message

If no record is found the not found message is shown
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.



Comments


bottom of page