PostgreSQL & PHP Tutorials - Deleting Data from your Database

PHP »  Deleting Data
PHP »  Starting Out »  Deleting Data
PostgreSQL »  Deleting Data
PostgreSQL »  Starting Out »  Deleting Data

Posted By Chris Smith Posted on 12 Feb 2006, 06:24 AM
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.
Avg Rating: 4
Vote Count: 22


              

  New Reply


Thanks for your tutorials, they have been an invaluable aid to me while learning the basics of postgresql and PHP. There are a couple of oversights in your 'Delete' tutorial - there is an extra close-body and close-html tag in the first script, plus a $POST instead of a $_POST in the second script. Your 'Add' and 'Update' tutorials worked perfectly for me, though I never saw a mention of how to create the sample database. I worked it out from your previous versions of these tutorials, but it may help others to explain the initial table/data creation. Many, many thanks :) D 28 Mar 2006 Reply
`-- Thanks for the comments :)

Bugs fixed up.

The related articles section includes how to create the database.. I should make that a bit more prominent :)
Chris Smith 29 Mar 2006 Reply

Nice tutorials helped me a lot.
Thx
Sumedh 01 Apr 2006 Reply

I am fresher in php , plz help me using hidden tags in HTML in conjunction with PHP & Javascript

[Editor's Note] - There are tons of sites out there to help you with this. http://www.webmonkey.com/ is one such example.
Tushar 06 May 2006 Reply


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 4 + 4 =
Comment