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.
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 :-
Now, we implement the stepwise process.
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
) ENGINE = INNODB DEFAULT CHARSET = latin1;
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.
<?php$databaseHost = 'localhost';
$databaseName = 'p4n';
$databaseUsername = 'root';
$databasePassword = '';
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
?>
1. Define your host(localhost ,127.0.0.1 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.
HTML Form
<form action="add.php" method="post" name="form1">
<table width="25%" border="0">
<tbody><tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>
</tbody></table>
</form>
CRUD application designing using CSS
You can design CRUD application form and buttons using CSS.
<style>
input[type=submit] {
width: 100%;
padding: 10px;
margin: 5px 0;
box-sizing: border-box;
border: 2px solid #000000;
outline: none;
background-color: green;
color:white;
font-size:20px;
}
.button{
width: 100%;
padding: 10px;
margin: 5px 0;
box-sizing: border-box;
border: 2px solid #000000;
outline: none;
underline:none;
background-color:#f44336;
color:white;
font-size:20px;
}
table tr{
color:white;
}
input[type=text] {
width: 50%;
padding: 9px ;
margin: 5px 0;
border: 1px solid #000000;
outline: none;
}
input[type=text]:focus {
background-color: lightblue;
}
</style>
Use above CSS according to your need.
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
<?phpinclude_once("config.php");
if(isset($_POST['Submit'])) {
$name = $_POST['name'];
$age = $_POST['age'];
$email =$_POST['email'];
$result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUE
('$name','$age','$email')");
echo "<font color="black">Data added successfully.";
echo "<br><a href='index.php'>View Result";
}
}
?></font>
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
<?php
include_once("config.php");
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
?>
<?php
while($res = mysqli_fetch_array($result)) {
echo "";
echo "";
echo "";
echo "";
echo "";
}
?>
<table width="50%" height="15%" border="0">
<tbody><tr bgcolor="yellow">
<td>Name</td>
<td>Age</td>
<td>Email</td>
<td>Update</td>
<td>Remove</td>
</tr><tr><td bgcolor="">".$res['name']."</td><td>".$res['age']."</td><td>".$res['email']."</td><td bgcolor="green"><a href="edit.php?id=$res[id]"><font color="white">Edit</a>";
echo"</font></td><td bgcolor="red"> <a href="delete.php?id=$res[id]" onClick="return confirm('Are you sure you want to delete?')"><font color="white">Delete</font></a></td></tr></tbody></table>
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
<?php
include_once("config.php");
if(isset($_POST['update']))
{
$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");
}
}
?>
<?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">
<table border="0">
<tbody><tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name;?>"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" value="<?php echo $age;?>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $email;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<?php" echo="" $_get['id'];?>=""></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</tbody></table>
</form>
Step 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)
<?php
include("config.php");
$id = $_GET['id'];
$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");
header("Location:index.php");
?>
Comments