Delete Data From Mysql Using PHP

Hello Guys Welcome to Coding Gurus Online Portal ...!

Till now we have learnt to Insert,Select and Update data and now we DELETE the exsisting data for some purpose.

Query

"DELETE FROM table_name WHERE column_name=' value' ";

Overview

In this process we create 2 files
1. delete.php
2. delete_ac.php

Steps

1. Create table test_mysql in database gurus.
2. Create file delete.php
3. Create file delete_ac.php

Setting Up the coding Table

CREATE TABLE `coding` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
);

Dumping data for table `coding`

INSERT INTO `coding` VALUES (1, 'Billly', 'Blueton', 'bb5@testingmail');
INSERT INTO `coding` VALUES (2, 'Jame', 'Campbell', 'jame@somewhere.com');
INSERT INTO `coding` VALUES (3, 'Mark', 'Jackson', 'mark@testingmail');
INSERT INTO `coding` VALUES (4, 'Linda', 'Travor', 'lin65@testingmail');
INSERT INTO `coding` VALUES (5, 'Joey', 'Ford', 'fordloi@somewhere.com');
INSERT INTO `coding` VALUES (6, 'Sidney', 'Gibson', 'gibson@testingmail');

Creating a file delete.php

<?php
$host="localhost";
// Host name
$username="coding"; // Mysql username
$password="coding"; // Mysql password
$db_name="codinggurus"; // Database name
$tbl_name="coding"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// select record from mysql
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>

<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>

<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF">&nbsp;</td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td>
</tr>

<?php
// close while loop
}
?>
</table>
<?php
// close connection
mysql_close();
?>

Creating a file delete_ac.php

<?php
$host="localhost";
// Host name
$username="coding"; // Mysql username
$password="coding"; // Mysql password
$db_name="codinggurus"; // Database name
$tbl_name="coding"; // Table name // Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
}

else {
echo "ERROR";
}
?>

<?php
// close connection
mysql_close();
?>