CRUD application using PHP and MYSQL database

CRUD stands for creating, read, update and delete. In this type of application, we perform the small parts of a web application. It is an essential process for every web application.CRUD means you have to insert some data into the database and fetch data from the database on the web page after that you can update and delete data from the webpage. We have to use the MYSQL database to create a CRUD application in PHP. CRUD is a system that can help insert, display, update and delete any user data.

PHP CRUD application in PHP and MYSQL

Steps to create CRUD application in PHP

You have to follow some steps to create a CRUD application through PHP and MYSQL .In the CRUD application, you create MYSQL database and perform other operations too.

Table of contents:-

1. Create an MYSQL database table.

2. Make a Connection file.

3.Insert data into a database table using PHP and MYSQL.

4. Display data from the MYSQL database table using HTML table and PHP script.

5. Update data in HTML table using PHP and MYSQL database.

6. Delete data from MYSQL database table in PHP.

Now, we implement the stepwise process.

1. We will create a database in MYSQL for insert ,read,update and delete operation.

2. Create a connection file to connect the database.

3. After that, we will perform an insert query to insert the data into the database. Insert operation is a must for CRUD application. Insert provides to user add some data into a database using HTML form and PHP code.

4. Read- The next process of CRUD application is Read data. Read means you have to fetch or display the data on the webpage.

5. The update operation is done by the update query. The update means you have to edit data on the webpage using HTML text boxes.

6. Delete operation is done by delete query. Delete means you want to remove the data from the MYSQL database table through a web application.

Step 1- Create an MYSQL database table.

First of all, we create an MYSQL database table for CRUD application using a PHP script. We will perform CRUD operations only on this database table. To create a MySQL database table, we can also execute the create query.

CREATE TABLE  `users` (
 `id` INT( 11 ) NOT NULL ,
 `name` VARCHAR( 100 ) NOT NULL ,
 `age` INT( 3 ) NOT NULL ,
 `email` VARCHAR( 100 ) NOT NULL

Step 2- Make a Connection file

Make a connection to create a CRUD application. Create a connection file with named config.php. A connection file helps in connect PHP script to the MYSQL database. After connection, we can easily create a CRUD application via PHP codes.

$databaseHost = 'localhost';
$databaseName = 'test';
$databaseUsername = 'root';
$databasePassword = '';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); 

1. Define your host(localhost , or global server host) .

2. Define your database name.

3. Define your username . (Root for localhost )

4. Define your databse password.(Blank for localhost )

Step 3- Insert data into a database table using PHP and MYSQL.

Insert operation is an important part of the CRUD application. You can not perform CRUD operations unless the data is inserted in the database. The data is first created in the database table. We use Insert operation to create data in the MYSQL database table. For an insert operation, we create an HTML form. The data through the HTML form is inserted into the database table.


<form action="add.php" method="post" name="form1">

CRUD application designing using CSS

You can design CRUD application form and buttons using CSS.



Use above CSS according to your need.

CRUD application insert operation using PHP and MYSQL

Now, we create a PHP script. When the user fills the form and clicks on the submit button ,all data should be saved in database.

1. Include the database configuration file.

2. If values are set then insert data into database.

3. Msqli_query – insert the data into database .

4. If the data save successfully then display the message “Data added successfully”

Insert PHP Code

if(isset($_POST['Submit'])) {	
$name = $_POST['name'];
$age = $_POST['age'];
$email =$_POST['email'];

$result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUE
echo "Data added successfully.";
echo "
<a href='index.php'>View Result"; } } ?>

Step 4- Display data from the MYSQL database table using HTML table and PHP script

After the insert data, we perform the code for display data from the database. This is the view part of the CRUD application. This is known as the reading operation of the CRUD application. Fetch the data and display data on the website page.
When the data insert after that the whole data will display in the HTML table automatically.

1. Include config file to connect the database.

2. Perform the select query for fetch the data from the database.

3. We use the HTML table. We display the data into table rows and columns from the database using mysqli_fetch_array() function.

4. We create a while loop to display the data from the database one by one row.

5. At the end of the code, we create id or delete and update. When a user clicks on update(edit) or deletes link then the id will help to edit or delete the one by one row that you have to choose to delete. (unique id for every row) .

Display data in table

$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); 

while($res = mysqli_fetch_array($result)) { 		
echo "";
echo "";
echo "";
echo "";	
echo "";        
Name Age Email Update Remove
".$res['name']."".$res['age']."".$res['email']."<a href="edit.php?id=$res[id]">Edit</a>"; echo" <a href="delete.php?id=$res[id]" onClick="return confirm('Are you sure you want to delete?')">Delete</a>

Step 5-Update data in database table using PHP and MYSQL

The update operation is an important operation of the CRUD application, with the help of which we update the MySQL database table data.CRUD application means that it is performing all kinds of operations on data. Here we will perform the update operation of the CRUD application. The Update operation helps to edit all inserted data.

1. Include connection file.

3. In the update operation, you have to need to select the data from the database and display it in text boxes.

4. After the data fetch, we perform the update query using id one by one.

Let's create a PHP script for select data and update data. The select query is used to display the data into HTML form text boxes and the update query is used to update kind of data from the HTML form. Create a PHP script
Create a PHP script

Update data PHP code


$id = $_POST['id'];
$name = $_POST['name'];
$age =  $_POST['age'];
$email =$_POST['email'];	
$result = mysqli_query($mysqli, "UPDATE users SET name='$name',age='$age',email='$email' WHERE id=$id");
header("Location: index.php");
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");
while($res = mysqli_fetch_array($result))
$name = $res['name'];
$age = $res['age'];
$email = $res['email'];

In the CRUD application, when you update the data, the data is first displayed in the HTML form, which can update the data. It is very easy to update the data in the HTML form. Let's make a form and select data into input boxes form the MYSQL database table.

Display and update from HTML form

<form name="form1" method="post" action="edit.php">

6. Delete data from MYSQL database table in PHP

This is the last operation of the crude application. By deletion operation, we delete the data from the database table. This is known as remove operation in PHP. 1. To Delete data from the database table, we use table row id, The id identifies the values of the row. If you have click on delete row number one the ID no 1 will be deleted. 2. Delete Query is used for deleting the data from the database.

Delete(Remove data)

$id = $_GET['id'];
$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");

Please Share

Recommended Posts:-