Insert Update Delete in PHP MySQL

Insert Update Delete in PHP MySQL

In this blog I will try to explain you, how you can implement crud operataions or insert, update, delete in PHP using mysql. 

First you have to learn about some basis of PHP variables, functions to follow this article. With the help of insert, update, edit in php using mysql, we can make our website dynamic. 

I'm using PHP queries to perform crud operation in the blog.

DEMO

Also Read: JQuery AJAX image upload with form data in PHP

Let's start by creating an database first, which I've named `crud_operation`. After creating the database the next step is create a table.

Create a Table

I've created a `users` table with columns id, name, email, address. Execute below SQL command to create table in phpmyadmin after selecting your database.

Execute below SQL command to create table in your database.

CREATE TABLE users (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    address VARCHAR(255) NOT NULL
);

 

After that we will create some files to perform crud operations

Also Read: How to make image background transparent using PHP

1. connection.php

2. index.php

3. insert.php

4. view.php

5. edit.php

6. update.php

7. delete.php

 

Copy paste below code in your files.

connection.php

<?php 
$con=mysqli_connect("localhost","root","","crud_operation"); 
if(!$con) { die(" Connection Error "); } 
?>

index.php

<h3> Registration Form in PHP</h3>
<form action="insert.php" method="post">
<input type="text" placeholder="Full Name" name="full_name"><br><br>
<input type="email" placeholder="Email" name="email"><br><br>
<input type="text" placeholder="Address" name="address"><br><br>
<button name="submit">Submit</button>
</form>

 

insert.php

<?php
require_once("connection.php");
    if(isset($_POST['submit']))
    {
        if(empty($_POST['full_name']) || empty($_POST['email']) || empty($_POST['address']))
        {
            echo 'All fields are required';
        }
        else
        {
            $UserName = $_POST['full_name'];
            $UserEmail = $_POST['email'];
            $UserAge = $_POST['address'];

            $query = "insert into users (name, email,address) values('$UserName','$UserEmail','$UserAge')";
            $result = mysqli_query($con,$query);
            if($result)
            {
                header("location:view.php");
            }
            else
            {
                echo 'Something went wrong';
            }
        }
    }
    else
    {
        header("location:index.php");
    }
?>

view.php

 <?php
    require_once("connection.php");
    $query = " select * from users ";
    $result = mysqli_query($con,$query);
?>
<a href="index.php">Add New</a>
<table class="table" border="1">
<tr>
<td> Name </td>
<td> Email </td>
<td> Address </td>
<td> Edit  </td>
<td> Delete </td>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$UserID = $row['id'];
$UserName = $row['name'];
$UserEmail = $row['email'];
$UserAge = $row['address'];
?>
<tr>
<td><?php echo $UserName; ?></td>
<td><?php echo $UserEmail; ?></td>
<td><?php echo $UserAge; ?></td>
<td><a href="edit.php?id=<?php echo $UserID; ?>">Edit</a></td>
<td><a href="delete.php?id=<?php echo $UserID; ?>">Delete</a></td>
</tr>        
<?php 
}  
?>                                                                    
</table>                 

edit.php

<?php
    require_once("connection.php");
    $UserID = $_GET['id'];
    $query = " select * from users where id='".$UserID."'";
    $result = mysqli_query($con,$query);

    while($row=mysqli_fetch_array($result))
    {
        $UserName = $row['name'];
        $UserEmail = $row['email'];
        $UserAge = $row['address'];
   }
?>
<h3> Update User</h3>
<form action="update.php" method="post">
<input type="hidden" name="user_id" value="<?php echo $UserID; ?>">
<input type="text" placeholder="Full Name" name="full_name" value="<?php echo $UserName; ?>"><br><br>
<input type="email" placeholder="Email" name="email" value="<?php echo $UserEmail; ?>"><br><br>
<input type="text" placeholder="Address" name="address" value="<?php echo $UserAge; ?>"><br><br>
<button name="submit">Update</button>
</form>

update.php

<?php
require_once("connection.php");
    if(isset($_POST['submit']))
    {
        if(empty($_POST['full_name']) || empty($_POST['email']) || empty($_POST['address']))
        {
            echo 'All fields are required';
        }
        else
        {
            $UserID = $_POST['user_id'];
            $UserName = $_POST['full_name'];
            $UserEmail = $_POST['email'];
            $UserAge = $_POST['address'];
            $query = " update users set name = '".$UserName."', email='".$UserEmail."',address='".$UserAge."' where id='".$UserID."'";
            $result = mysqli_query($con,$query);
            if($result)
            {
                header("location:view.php");
            }
            else
            {
                echo 'Something went wrong';
            }
        }
    }
    else
    {
        header("location:index.php");
    }
?>

delete.php

<?php
require_once("connection.php");
    $UserID = $_GET['id'];
$query = " delete from users where id = '".$UserID."'";
    $result = mysqli_query($con,$query);
if($result)
{
header("location:view.php");
}
else
{
echo 'Something went wrong';
}
?>

 

In this blog I will try to explain you, how you can implement crud operataions or insert, update, delete in PHP using mysql. 

First you have to learn about some basis of PHP variables, functions to follow this article. With the help of insert, update, edit in php using mysql, we can make our website dynamic. 

I'm using PHP queries to perform crud operation in the blog.

DEMO

Also Read: JQuery AJAX image upload with form data in PHP

Let's start by creating an database first, which I've named `crud_operation`. After creating the database the next step is create a table.

Create a Table

I've created a `users` table with columns id, name, email, address. Execute below SQL command to create table in phpmyadmin after selecting your database.

Execute below SQL command to create table in your database.

CREATE TABLE users (
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    address VARCHAR(255) NOT NULL
);

 

After that we will create some files to perform crud operations

Also Read: How to make image background transparent using PHP

1. connection.php

2. index.php

3. insert.php

4. view.php

5. edit.php

6. update.php

7. delete.php

 

Copy paste below code in your files.

connection.php

<?php 
$con=mysqli_connect("localhost","root","","crud_operation"); 
if(!$con) { die(" Connection Error "); } 
?>

index.php

<h3> Registration Form in PHP</h3>
<form action="insert.php" method="post">
<input type="text" placeholder="Full Name" name="full_name"><br><br>
<input type="email" placeholder="Email" name="email"><br><br>
<input type="text" placeholder="Address" name="address"><br><br>
<button name="submit">Submit</button>
</form>

 

insert.php

<?php
require_once("connection.php");
    if(isset($_POST['submit']))
    {
        if(empty($_POST['full_name']) || empty($_POST['email']) || empty($_POST['address']))
        {
            echo 'All fields are required';
        }
        else
        {
            $UserName = $_POST['full_name'];
            $UserEmail = $_POST['email'];
            $UserAge = $_POST['address'];

            $query = "insert into users (name, email,address) values('$UserName','$UserEmail','$UserAge')";
            $result = mysqli_query($con,$query);
            if($result)
            {
                header("location:view.php");
            }
            else
            {
                echo 'Something went wrong';
            }
        }
    }
    else
    {
        header("location:index.php");
    }
?>

view.php

 <?php
    require_once("connection.php");
    $query = " select * from users ";
    $result = mysqli_query($con,$query);
?>
<a href="index.php">Add New</a>
<table class="table" border="1">
<tr>
<td> Name </td>
<td> Email </td>
<td> Address </td>
<td> Edit  </td>
<td> Delete </td>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$UserID = $row['id'];
$UserName = $row['name'];
$UserEmail = $row['email'];
$UserAge = $row['address'];
?>
<tr>
<td><?php echo $UserName; ?></td>
<td><?php echo $UserEmail; ?></td>
<td><?php echo $UserAge; ?></td>
<td><a href="edit.php?id=<?php echo $UserID; ?>">Edit</a></td>
<td><a href="delete.php?id=<?php echo $UserID; ?>">Delete</a></td>
</tr>        
<?php 
}  
?>                                                                    
</table>                 

edit.php

<?php
    require_once("connection.php");
    $UserID = $_GET['id'];
    $query = " select * from users where id='".$UserID."'";
    $result = mysqli_query($con,$query);

    while($row=mysqli_fetch_array($result))
    {
        $UserName = $row['name'];
        $UserEmail = $row['email'];
        $UserAge = $row['address'];
   }
?>
<h3> Update User</h3>
<form action="update.php" method="post">
<input type="hidden" name="user_id" value="<?php echo $UserID; ?>">
<input type="text" placeholder="Full Name" name="full_name" value="<?php echo $UserName; ?>"><br><br>
<input type="email" placeholder="Email" name="email" value="<?php echo $UserEmail; ?>"><br><br>
<input type="text" placeholder="Address" name="address" value="<?php echo $UserAge; ?>"><br><br>
<button name="submit">Update</button>
</form>

update.php

<?php
require_once("connection.php");
    if(isset($_POST['submit']))
    {
        if(empty($_POST['full_name']) || empty($_POST['email']) || empty($_POST['address']))
        {
            echo 'All fields are required';
        }
        else
        {
            $UserID = $_POST['user_id'];
            $UserName = $_POST['full_name'];
            $UserEmail = $_POST['email'];
            $UserAge = $_POST['address'];
            $query = " update users set name = '".$UserName."', email='".$UserEmail."',address='".$UserAge."' where id='".$UserID."'";
            $result = mysqli_query($con,$query);
            if($result)
            {
                header("location:view.php");
            }
            else
            {
                echo 'Something went wrong';
            }
        }
    }
    else
    {
        header("location:index.php");
    }
?>

delete.php

<?php
require_once("connection.php");
    $UserID = $_GET['id'];
$query = " delete from users where id = '".$UserID."'";
    $result = mysqli_query($con,$query);
if($result)
{
header("location:view.php");
}
else
{
echo 'Something went wrong';
}
?>

 

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

lasixkiP
lasixkiP
06 Dec 2022

I am not sure where you’re getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for excellent information I was looking for this information for my mission.

Bryanphife
Bryanphife
01 Feb 2024

Thanks for excellent information. What trips can you recommend in 2024? Astro tourism, eco diving, home swapping, train stations are the new food destinations,sports tourism, coolcationing, gig tripping, private group travel?

Add your comment

Close