0% found this document useful (0 votes)
51 views

Deleting Data From Mysql

This document provides instructions for deleting data from a MySQL database using PHP. It describes creating a table called "test_mysql" with sample data. It then shows how to create two PHP files: delete.php displays the table data and includes delete links, and delete_ac.php deletes the row when the corresponding link is clicked by retrieving the ID from the URL and running a DELETE query. The DELETE syntax and retrieving/deleting a row by ID are the focus.

Uploaded by

Almeda Asuncion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Deleting Data From Mysql

This document provides instructions for deleting data from a MySQL database using PHP. It describes creating a table called "test_mysql" with sample data. It then shows how to create two PHP files: delete.php displays the table data and includes delete links, and delete_ac.php deletes the row when the corresponding link is clicked by retrieving the ID from the URL and running a DELETE query. The DELETE syntax and retrieving/deleting a row by ID are the focus.

Uploaded by

Almeda Asuncion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Deleting data From mysql

Delete unwanted data from your mysql database.

Syntax

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


Overview
In this tutorial create 2 files
1. delete.php
2. delete_ac.php
Step
1. Create table "test_mysql" in database "test".
2. Create file delete.php.
3. Create file delete_ac.php.
Set up database

CREATE TABLE `test_mysql` (


`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`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;
--- Dumping data for table `test_mysql`
-INSERT INTO `test_mysql`
'[email protected]');
INSERT INTO `test_mysql`
'[email protected]');
INSERT INTO `test_mysql`
'[email protected]');
INSERT INTO `test_mysql`
'[email protected]');
INSERT INTO `test_mysql`
'[email protected]');

VALUES (1, 'Billly', 'Blueton',


VALUES (2, 'Jame', 'Campbell',
VALUES (3, 'Mark', 'Jackson',
VALUES (4, 'Linda', 'Travor',
VALUES (5, 'Joey', 'Ford',

INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson',


'[email protected]');

Create file - delete.php


View In Browser / Diagram

############### Code
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // 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" cellspacing="1" cellpadding="0">
<tr>
<td><table width="400%" border="0" cellpadding="3"
cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" 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>
<?

// close while loop


}
// close connection;
mysql_close();
?>
</table></td>
</tr>
</table>
Create file delete_ac.php

############### Code
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // 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";
echo "<BR>";
echo "<a href='delete.php'>Back to main page</a>";
}
else {

echo "ERROR";
}
// close connection
mysql_close();
?>

You might also like