0% found this document useful (0 votes)
4 views4 pages

Lec16Deleting and Updating Records

This document explains how to delete and update records in a database using SQL and PHP. It provides syntax and examples for the DELETE and UPDATE SQL commands, as well as PHP scripts to execute these commands. Additionally, it discusses how to send data through links and retrieve it using the $_GET super global array in PHP.

Uploaded by

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

Lec16Deleting and Updating Records

This document explains how to delete and update records in a database using SQL and PHP. It provides syntax and examples for the DELETE and UPDATE SQL commands, as well as PHP scripts to execute these commands. Additionally, it discusses how to send data through links and retrieve it using the $_GET super global array in PHP.

Uploaded by

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

Deleting 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>

2. Updating the record:


Updating the data is also an important function, we perform on data. SQL provides us the
update instruction to update some existing data. The syntax of the update instruction is
UPDATE table-name
SET column-names = values
WHERE condition
In this instruction UPDATE is a keyword, table-name is the name of the table where we want to
update some data, SET is also a keyword, column-names are the names of the columns in the
table, values are new values for columns, WHERE is a keyword; and condition is the condition
about the record to be updated. In the following example we update a record whose id is 1 in
the users table
UPDATE users
SET user_Name = ‘Ali’,
user_Email = ‘[email protected]’,
user_Password=‘123’
WHERE user_Id=1
To update a record using PHP, first of all we make a connection with database and then
execute the update instruction as shown below
<?php
mysql_connect(‘localhost’,’root’,’’);
mysql_select_db(‘testdatabase’);
$sql =“UPDATE users
SET user_Name = ‘Ali’,
user_Email = ‘[email protected]’,
user_Password=‘123
Where user_Id=1’’;
mysql_query($sql);
?>

You might also like