Lec16Deleting and Updating Records
Lec16Deleting and Updating Records
In previous lectures, we discussed some basic function which we perform on data including
data insertion and data selection. In this lecture we will learn how we can delete data from
database and how we can update the existing data.
1. Deleting data:
Deletion is the process of removing data from database. When we feel that certain data is no
more required, we remove that data in-order to reduce the size of the database. SQL provides
the delete instruction to delete records from the database. The syntax of the delete instruction
is
DELETE FROM table-name
WHERE condition
Here, DELETE FROM is the keyword while table-name is the name of the table from which we
want to delete data. WHERE is also a keyword and here, we give the condition about that which
record is to be deleted. For example the following code deletes the record of the user whose id
is 5 from the users table (we used in previous lecture)
DELETE FROM users
WHERE user_Id =5
We can delete data from the database using PHP. For this, first of all we make a connection to
the database and then we execute the delete instruction. The following example shows the PHP
scripts that deletes the record of the user whose id is 5 from the ‘users’ table of the
’testdatabase’ database.
<?php
mysql_connect(‘localhost’,’root’,’’);
mysql_select_db(‘testdatabase’);
$sql=“DELETE FROM users
WHERE user_Id=5”;
mysql_query($sql);
?>
Sending data with link: while studying HTML, we discussed how we can link documents with
each other. Sometimes we have to send some data with a link. We can attach data with a link as
<a href=”page-reference?data-reference=”value””> …….. </a>
The attached data is sent along with the URL as a query string parameter. As we know, in PHP
$_GET super global array keeps the data which is attached with the URL. Therefore we can get
this data on the next page. In the above line of coed, the page reference is the reference of the
page where we want to move, data-reference is the name of the field of $_GET array where the
data is stored and value is the actual data. On next page we can get this value by $_GET[‘data-
reference’].
In the following example we display the record of the users from the ‘users’ table (as we did in
the previous lecture) and add a new column which links to a page ’delete.php’. With this link
we attach the id of the user. The delete.php gets the id of the user from the link and deletes
that record.
<html> <?php
<head> $id = $_GET['id'];
<title> Retrieving data</title> include('connection.php');
</head> $sql="delete from users
<body> where user_Id=$id";
<?php mysql_query($sql);
mysql_connect(‘localhost’,’root’,’’); ?>
mysql_select_db(‘testdatabase’);
$sql = "select * from users";
$result = mysql_query($sql);
?>
<table border="1">
<trbgcolor="#CCCC00">
<th>User Name</th>
<th>User Email</th>
<th>User Password</th>
<th>User Picture</th>
<th>Action</th>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<trbgcolor="#CCCCCC">
<td><?php echo $rows[1]; ?></td>
<td><?php echo $rows[2]; ?></td>
<td><?php echo $rows[3]; ?></td>
<td><imgsrc="<?php echo $rows[4]; ?>"
height="100" width="100"></td>
<td>
<a href="delete.php?id=<?php echo $rows[0];?>"
onClick="return confirm('Delete This account?')">
Delete</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>