Web DB Tutorial : Manage Data
Once we have collected data in the database the links may get old and need updating or the links just may no longer work. So we need to be able to update and/or delete the data from the database.
There are two SQL statements which handle each of these, and they are coincidently named UPDATE and DELETE.
The format for the UPDATE statement is:
(column1) = (value),
(column2) = (value),
...
WHERE (column) = (value)
The DELETE statement format is:
WHERE (column) = (value)
A good example of how to manage links is at any of the portal personal pages, such
my.yahoo.com.
These sites display the links entered in, and you then click to edit or delete the one you want.
To do this you first need to use a SELECT statment to display the available links.
The code is the same as before:
$category = "Local Docs";
$sql = " SELECT * FROM links ";
$sql .= " WHERE category = '$category' ";
$rs = mysql_query($sql, $cid);
?>
The delete code is shown below, I will leave the edit page as an exercise for you to do. The edit page is only slightly more complex because you need another form to display the data to edit. This can be processed the same way as the insert script. What I usually do is copy over the insert form and modify that, it is a good base for my edit page.
Code to display the links on the screen:
while ($row = mysql_fetch_array($rs)) {
$sitename = $row["sitename"];
$id = $row["id"];
print "<tr>";
print "<td>$sitename</td>\n";
print "<td><a href='manageedit.php?id=$id'>Edit</a></td>";
print "<td><a href='manage.php?id=$id&task=del'>Delete</a></td>";
print "</tr>";
}
print "</table>";
When the "Edit" link is clicked it will go to the "manageedit.php" page, it will also pass along the id of the link to edit. This page will display the data in a form. When that form is submitted it will update the information in the form.
When the "Delete" link is clicked it will submit to the same page (manage.php) and pass along two variables.
The first variable is the id of the record to delete, the second variable is "task" which is set to "del". This
is done so we can catch that variable and know we are performing a delete action when we load the page. The
following code, which is placed in the top of the script, shows how:
$sql = " DELETE FROM links ";
$sql .= " WHERE id = $id ";
$sql .= " LIMIT 1 ";
mysql_query($sql, $cid);
}
As an extra safe guard, I usually add the LIMIT 1. This restricts the SQL statement to only effect one row.
The manage script can be download here (manage.phps) and though I left the manageedit.php page as an exercise, which you should try to create first, you can download that page here. (manageedit.phps)
This is the end of the beginner web database tutorial. I plan on adding more advanced tutorials and other tips. Look around the site and check back later.
I hope you found it useful.
Web Database Development Resources
The collection of links to books and other sites which may be helpful for
continuing web application development using PHP and MySQL.
| Sections: | Getting Started | Create Database | Insert Data | View Data | Manage Data | Resources |