In this blog post we will illustrate you how to delete data from database using PHP. For this you must have a database in MYSQL where your record are present.


In MySQL the command used is :

DELETE FROM table_name
WHERE column_name=some_value;

and in php query used is :

$query1 = mysql_query("delete from employee where employee_id=$del", $connection);

Here, we have database named “company” which consists of table named “employee” with 5 fields viz.

“employee_id”, “employee_name”, “employee_email”, “employee_contact” and “employee_address”.

Next we created a php page named “updatephp.php” where following steps were performed:

    • We first establish connection with server .
    • Selects database.
    • Executes SQL query.
    • Closing connection with server.

Below is our complete code with download and live demo option


PHP File: deletephp.php


<!DOCTYPE html>
<html>
<head>
<title>Delete Data Using PHP- Demo Preview</title>
<meta content="noindex, nofollow" name="robot">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="maindiv">
<div class="divA">
<div class="title">
<h2>Delete Data Using PHP</h2>
</div>
<div class="divB">
<div class="divD">
<p>Click On Menu</p>
<?php
$connection = mysql_connect("localhost", "root", ""); // Eastablishing Connection With Server.
$db = mysql_select_db("company", $connection); // Selecting Database From Server.
if (isset($_GET['del'])) {
$del = $_GET['del'];
//SQL query for deletion.
$query1 = mysql_query("delete from employee where employee_id=$del", $connection);
}
$query = mysql_query("select * from employee", $connection); // SQL query to fetch data to display in menu.
while ($row = mysql_fetch_array($query)) {
echo "<b><a href="deletephp.php?id={$row['employee_id']}">{$row['employee_name']}</a></b>";
echo "<br />";
}
?>
</div><?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
// SQL query to Display Details.
$query1 = mysql_query("select * from employee where employee_id=$id", $connection);
while ($row1 = mysql_fetch_array($query1)) {
?>
<form class="form">
<h2>---Details---</h2>
<span>Name:</span> <?php echo $row1['employee_name']; ?>
<span>E-mail:</span> <?php echo $row1['employee_email']; ?>
<span>Contact No:</span> <?php echo $row1['employee_contact']; ?>
<span>Address:</span> <?php echo $row1['employee_address']; ?><
<?php echo "<b><a href="deletephp.php?del={$row1['employee_id']}"><input type="button" class="submit" value="Delete"/></a></b>"; ?>
</form><?php
}
}
// Closing Connection with Server.
mysql_close($connection);
?>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</body>
</html>

 

MY-SQL Code Segment:

Here is the My-SQL code for creating database and table.


CREATE DATABASE company;
CREATE TABLE employee(
employee_id int(10) NOT NULL AUTO_INCREMENT,
employee_name varchar(255) NOT NULL,
employee_email varchar(255) NOT NULL,
employee_contact varchar(255) NOT NULL,
employee_address varchar(255) NOT NULL,
PRIMARY KEY (employee_id)
)

 

 CSS File: style.css


@import "http://fonts.googleapis.com/css?family=Droid+Serif";
/* Above line is to import Google font style */
.maindiv{
margin:0 auto;
width:980px;
height:500px;
background:#fff;
padding-top:20px;
font-size:14px;
font-family:'Droid Serif',serif
}
.title{
width:100%;
height:70px;
text-shadow:2px 2px 2px #cfcfcf;
font-size:16px;
text-align:center;
font-family:'Droid Serif',serif
}
.divA{
width:70%;
float:left;
margin-top:30px
}
.form{
width:400px;
float:left;
background-color:#fff;
font-family:'Droid Serif',serif;
padding-left:30px
}
.divB{
width:100%;
height:100%;
background-color:#fff;
border:dashed 1px #999
}
.divD{
width:200px;
height:480px;
padding:0 20px;
float:left;
background-color:#f0f8ff;
border-right:dashed 1px #999
}
p{
text-align:center;
font-weight:700;
color:#5678C0;
font-size:18px;
text-shadow:2px 2px 2px #cfcfcf
}
.submit{
color:#fff;
border-radius:3px;
background:#1F8DD6;
padding:4px;
margin-top:40px;
border:none;
width:100px;
height:30px;
box-shadow:0 0 1px 1px #123456;
font-size:16px;
cursor:pointer
}
.form h2{
text-align:center;
text-shadow:2px 2px 2px #cfcfcf
}
a{
text-decoration:none;
font-size:16px;
margin:2px 0 0 30px;
padding:3px;
color:#1F8DD6
}
a:hover{
text-shadow:2px 2px 2px #cfcfcf;
font-size:18px
}
.clear{
clear:both
}
span{
font-weight:700
}

Conclusion:

We have shown you how Delete command of sql is executed with PHP for removing data from database , for more MYSQL commands with php you can refer to our blog posts.

For more related information go through following blogs –