Delete data from the table using PHP and MYSQL database

In this tutorial, we are going to perform the deletion operation. We create a PHP script to delete data from a database table. First of all, create a database table for performing the delete operation. After creating the database, we insert some data into the table. After inserting the data in the database table, the data can be deleted by the column id. In this process , we use HTML table.Display data into HTML table and delete by MYSQL delete query.

Create a database table

First of all, we create a database table. You can use MYSQL create table query. This will help you get remove data from the MYSQL database table using id. Delete data by id. Let's perform the create table query. Go to your PHP my admin panel and execute this query.

    CREATE TABLE `tutorials` (
  `id` int(40) NOT NULL,
  `Name` varchar(40) NOT NULL,
  `Email` varchar(30) NOT NULL,
  `mobile` varchar(200) NOT NULL
);

Insert data into a database for delete operation

You can not delete data from any table when data is not inserted. Only data can be deleted after data is inserted in the MYSQL table. There are 2 ways to insert the information into the database.

1.Insert data by MYSQL query and delete .

To insert data into the database table, you can perform the mysql insert query.

INSERT INTO `tutorials`(`id`, `Name`, `Email`, `mobile`) VALUES (1,'Mohan','mohan@example.com',9090909012);

2.Create a form to insert and display data in HTML table

In the previous tutorial, we had inserted the data into MYSQL database table and displayed into the HTML table. You can create an insert form to store the information in the database. Insert data into database and display in HTML Table using PHP & MYSQL
1. Create a HTML form and perform insert query .
2. Make HTML table and display data from database table.
3. Now perform the delete query.

Create an HTML table to select data and delete

Now we create an HTML form. To display the data in the table, we create the form. Before this tutorial, we had inserted the data and displayed it in the HTML table. After displaying the data we can easily delete it by id.

Make a connection to perform delete operation

Create a connection file . A connection file helps in connect PHP script to MYSQL database. After connection ,we can easiley insert data ,display data and delete by id. Save this file with name "config.php".

<?php

$host = '127.0.0.1';
$dbName = 'dbhandle'; 
$dbUsername = 'root'; 
$dbPassword = '';  


$mysqli = mysqli_connect($host, $dbUsername, $dbPassword, $dbame); ?>

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 )

Create PHP script for delete operation in HTML table

After data inserts and displays, you can easily delete the data. We create a delete link and a button inside the HTML table. With the help of that link, we delete the data through id. To perform a DELETE operation, we use the MYSQL delete query.

Delete data by id

This is the most important and easy think to delete data by id. Create a MySQL delete query and use where clause condition.

DELETE FROM `tutorials` WHERE id = 1;

The id may be anything. We create a link to get id from database table and delete it. Let's get a unique id and delete.

Delete file

<?php
 include("config.php"); 
//getting id of the data from url
$id = $_GET['id'];
 
//deleting the row from table
$result = mysqli_query($mysqli, "DELETE FROM crud2 WHERE id=$id");
 
//redirecting to the display page (index.php in our case)
header("Location:crud2.php");
?>


1. In the above PHP script, we have used an 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.



Download the free source code here. (Insert, Display, and Delete)

Recommended Posts:-

Previous Posts:-