PHP: CRUD Application |Create,Read,Update,Delete |PHP CRUD Example

Techno PHP

CRUD stands for Create Read Update and Delete. In this type of application, we will 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.

PHP CRUD Operation Now we will implement the stepwise process.

1. We will create a database in MYSQL for insert (insert),read(display) ,update(edit)and delete operation .
2. Create a connection file to connect the database.

3. After we will perform insert query to insert the data into the database. Insert is a must for CRUD application. Insert provides to user add some data into a database using HTML form.

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 operationis done by update query. The update means you have to edit data on the webpage using HTML textboxes.

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


Now we will implement these steps one by one –


1. First of all we will create a database structure –

Database Name- test

Table-

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;

2. Database connectivity –

Now create a database configuration file that helps you to database connectivity . We will create variables for values .

Like -

$databaseHost = 'localhost';        (localhost is host name stored in $databaseHost variable )

$databaseName = 'test';        ( test is a database name )

$databaseUsername = 'root';         ( root is a username )

$databasePassword = '';         (Your database password )


We will use mysqli library . Mysqli_connect takes four arguments -

1. DatabaseHost 2 .Database Name 3. Database UserName 4. Database Password .
If password is not set then default password will be empty .

config.php


<?php

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

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

3. Insert –

As you know that insert operation is done by insert query . Now we will implement operation. Now we create an HTML form. Inside the Html form, we will include some fields like -
Name
Age
Email

HTML Form

<form action="add.php" method="post" name="form1">
		
Name
Age
Email
</form>

Form action is very essential to perform the functionality of every we application . Now we will create a PHP script . When the user fill the form and click on the submit button all data will be saved in database ,

1. Include the database configuration file.

2. If value is 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


<?php
include_once("config.php");
if(isset($_POST['Submit'])) {	
	$name = mysqli_real_escape_string($mysqli, $_POST['name']);
	$age = mysqli_real_escape_string($mysqli, $_POST['age']);
	$email = mysqli_real_escape_string($mysqli, $_POST['email']);
		$result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");
		echo "Data added successfully.";
		echo "
<a href='index.php'>View Result"; } } ?>

Read operation (fetach/display data on the web) –

After the insert data we will perform the code for display(fetch) data from the database . The HTML an PHP table code is here . When the data saved then the data will display in table automatically .

1. Include config file to connect the database .

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

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

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

5. At the end of code, we will 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 rows) .

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 "";        
	}
	?>
	
Name Age Email Update Remove
".$res['name']."".$res['age']."".$res['email']."<a href=\"edit.php?id=$res[id]\">Edit"; echo" <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete

Upadate (Edit data in the table ) . –

Now we will perform the code to Update the data in the database.
1. include database file .

3. In update web application you have to need to select the data from the database and display in text boxes.

4. After the data fetch we will perform the update queryusing id one by one.

Update data(PHP and HTML form code)


 	<?php
include_once("config.php");

if(isset($_POST['update']))
{
	$id = mysqli_real_escape_string($mysqli, $_POST['id']);
	$name = mysqli_real_escape_string($mysqli, $_POST['name']);
	$age = mysqli_real_escape_string($mysqli, $_POST['age']);
	$email = mysqli_real_escape_string($mysqli, $_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'];
}
?>
<form name="form1" method="post" action="edit.php">
		
Name
Age
Email
>
</form>

Delete (Remove data from the database ). –

Now we will talk about how to delete data from the data base using table . To delete data from the database you have to need perform delete query . We will talk about the PHP script (PHP code ) .

1. To Delete script we have used a id , The id is identify the rows values . If you have click on delete row number one the ID no 1 is will be delete .

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");
?>
Download the source code here.