PostgreSQL & PHP Tutorials – Deleting Data from your Database

Deleting old data from your database is a useful thing to do. If you keep a list of books or cd’s you own and you sell or give something away, you can remove it from the list.

First of all, we need to set up a form that will ask us what we want to delete –


<html>
<body>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
Friend ID
</td>
<td>
First Name
</td>
<td>
Surname
</td>
<td>
Email Address
</td>
</tr>
<?php
$db = pg_connect('host=localhost dbname=contacts user=contacts password=firstphp');

$query = "SELECT * FROM friends";

$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}

while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['id'], htmlspecialchars($myrow['firstname']), htmlspecialchars($myrow['surname']), htmlspecialchars($myrow['emailaddress']));
}
?>
</table>
</body>
</html>

Just type in the ID number to delete.


<form action="delete.php" method="post">
ID to Delete : <input type="text" name="id" size="4" length="4" value=""><br>
<input type="submit" name="submit" value="Delete It">
<input type="reset" name="reset" value="Clear the Form">
</form>

This is almost the same form as we used for the previous example, with only the “form action” changing, and we only
need to put in the ID number. Again, we’re displaying the whole database just so it’s easier for us to work out what we’re going
to delete. Next, our delete script.


<html>
<body>
<?php
$db = pg_connect('host=localhost dbname=contacts user=contacts password=firstphp');
$id = (int)$_POST['id'];
$query = "DELETE FROM friends where id='$id'";
$result = pg_query($query);
if (!$result) {
printf ("ERROR");
$errormessage = pg_errormessage($db);
echo $errormessage;
exit();
}
printf ("Deleted from the database successfully");
pg_close();
?>
</body>
</html>

Pretty simple isn’t it? We’ll just look at our “delete” command in a bit more detail.


<?php
$query = "DELETE FROM friends where id='$id'";
?>

The format of the command is pretty simple –

DELETE from table where table1=value1

It’s pretty simple, we just tell it which ID to delete, and it deletes all of the information for that particular ID. If we didn’t tell it what ID to delete, it would delete everything, so make sure you give it an ID or something else to reference!

Note, this won’t tell you that the ID doesn’t exist. If you put in, for example, 99 for the ID, and hit submit, it would tell you that it had successfully deleted it from the database.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: