PHP Delete Data From MySQL

this tutorial help you to delete the inserted data from mysql using php that displayed in webpage, using php functions and mysql queries it’s so easy to delete the the data from mysql database table, if you want to set javascript powered confirmation to delete the data check this tutorial, delete data with confirmation, first of all select the data from mysql table and display the mysql rows into html table along with deletion link with anchor tag id as a query string, have a look at this tutorial.

Delete Data from MySQL in PHP

In MySQL  The Command Used as Follow :


DELETE FROM table_name
WHERE column_name=any_value;

And the PHP Query Would Be :

$query = mysql_query("delete from table_name where column_name=any_value");

Here i am using following MySql Database and Table to illustrate How To execute Sql Query to Delete the Data from MySql Database

Database : dbtuts
Table : users


CREATE DATABASE `dbtuts` ;
CREATE TABLE `dbtuts`.`users` (
`user_id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`first_name` VARCHAR( 25 ) NOT NULL ,
`last_name` VARCHAR( 25 ) NOT NULL ,
`user_city` VARCHAR( 45 ) NOT NULL
) ENGINE = InnoDB;

For This Insert Some Data Into MySql above created table
And Then we Need to Fetch Some Records From MySql table.

delete.php


<?php
mysql_connect("localhost","root","");
mysql_select_db("dbtuts");
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
?>
<table align="center">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
<th>Delete</th>
</tr>
<?php
if(mysql_num_rows($result_set)>0)
{
    while($row=mysql_fetch_row($result_set))
    {
            ?>
            <tr>
            <td><?php echo $row[1]; ?></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            <td align="center"><a href="delete.php?delete_id=<?php echo $row[0]; ?>"><img src="b_drop.png" alt="Delete" /></a></td>
            </tr>
            <?php
    }
}
else
{
        ?>
        <tr>
        <th colspan="4">There's No data found !!!</th>
        </tr>
        <?php
}
?>
</table>

Following is the QueryString Set Condition
If The QueryString set as

http://localhost/folder_name/delete.php?delete_id=1

it means the condition was set and the Sql Query will execute and Delete the 1 Record From MySql Table.


<?php
if(isset($_GET['delete_id']))
{
     $sql_query="DELETE FROM users WHERE user_id=".$_GET['delete_id'];
     mysql_query($sql_query);
     header("Location: delete.php");
}
?>

Conclusion:

I have shown you that how to Delete Records From MySql Database Table Using PHP.
That’s it .

Here’s the Complete Script

delete.php


<?php
mysql_connect("localhost","root","");
mysql_select_db("dbtuts");
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
if(isset($_GET['delete_id']))
{
 $sql_query="DELETE FROM users WHERE user_id=".$_GET['delete_id'];
 mysql_query($sql_query);
 header("Location: index.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Delete Data From MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>

<div id="header">
 <div id="content">
    <label>PHP Delete Data From MySql - By Cleartuts</label>
    </div>
</div>
<div id="body">
 <div id="content">
    <table align="center">
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>City</th>
    <th>Delete</th>
    </tr>
    <?php
 if(mysql_num_rows($result_set)>0)
 {
  while($row=mysql_fetch_row($result_set))
  {
   ?>
            <tr>
            <td><?php echo $row[1]; ?></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            <td align="center"><a href="index.php?delete_id=<?php echo $row[0]; ?>"><img src="b_drop.png" alt="Delete" /></a></td>
            </tr>
            <?php
  }
 }
 else
 {
  ?>
        <tr>
        <th colspan="4">There's No data found !!!</th>
        </tr>
        <?php
 }
 ?>
    </table>
    </div>
</div>

<div id="footer">
 <div id="content">
    <hr /><br/>
    <label>for more tutorials and blog tips visit <a href="http://cleartuts.blogspot.com"> : cleartuts.com</a></label>
    </div>
</div>

</center>
</body>
</html>

Stylesheet : style.css


@charset "utf-8";
/* CSS Document */
*
{
 margin:0;
 padding:0;
}
body
{
 background:#f9f9f9;
 font-family:"Courier New", Courier, monospace;
}
#header
{
 width:100%;
 height:50px;
 background:#00a2d1;
 color:#f9f9f9;
 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
 font-size:35px;
 text-align:center;
}
#body
{
 margin-top:80px;
}
table
{
 width:40%;
 font-family:Tahoma, Geneva, sans-serif;
 font-weight:bolder;
 color:#999;
}
table,td,th
{
 border-collapse:collapse;
 border:solid #d0d0d0 1px;
 padding:20px;
}
#footer
{
 margin-top:100px;
 font-weight:bolder;
 color:#00a2d1;
 font-family:Verdana, Geneva, sans-serif;
 font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
 font-family:"Courier New", Courier, monospace;
 font-size:20px;
}
#footer a
{
 color:#004567;
 text-decoration:none;
}
#footer a:hover
{
 color:brown;
}
#footer hr
{
 width:80%;
}