This is Part 4 of a tutorial designed to teach you how to create a basic CRUD (Create, Read, Update & Delete) interface for a MySQL table using PHP. Part 4 covers Delete.

Part 1: Create (Add)
Part 2: Read
Part 3: Update (Edit)

Create a file called delete.php, which will be very similar to edit.php. Include the config file as before. Our delete page will have a confirmation screen, we want the user to be sure this is the data they meant to delete. Before we display the confirmation form, we’ll need to select the relevant information, based off the unique ID we submitted in the URL. So we’ll check to see that it was submitted and that it’s a valid ID, just like we did with edit.php

Processing Data

The processing for delete.php will be much simpler than edit.php.

HTML Form

Finally, the HTML form. This one will also be simpler than the edit one, we only need to show the data, now allow changes. We will however want to make a change so we don’t show the form again after it’s deleted, since we already deleted it!

Conclusion

So now you should understand the basics of using basic queries and prepared statements in PDO to insert, select (read), update and delete data while keeping your database safe from SQL Injection, as well as processing form data in PHP. You could try adding some more validation rules, or try something harder like adding profile photos for your contacts, or e-mail addresses to send emails.

There will be a follow-up to this tutorial with information on ways to improve this basic tutorial, such as creating functions or even converting this application to OOP. If you’ve had any questions or comments, please post them, and I’ll be happy to respond as quickly as possible.

, , , , , , ,
Trackback

8 comments

  1. Nice tutorial. thanks for sharing.

  2. Thanks for this great tutorial.

    Personally – it would be great to be able to put all this in some kind of wrapper so it was all on one page. Is that something that you can help us with?

  3. Hi..your tutorial was very helpful. :) Thank you..I just want to ask. How to create a class for creating, updating and deleting of records in PDO?

  4. Great Tutorial. Only one or two questions: The SELECT statement in index.php includes the contact_id in the $fields_str variable and subsequently displays it in the grid when the foreach is processed, yet the #fields array in config.php doesn’t include it, causing an offset of one column when the grid displays.

  5. There was an error on line 31 of the index.php – this has been corrected.
    It should read:

    Making the cells show only the fields we desire. Hope that helps!

  6. Your mode of telling all in this piece of writing is really nice, all be capable of simply understand it,
    Thanks a lot.

  7. How about we add ‘contact_id’ => ‘Contact ID’ in the config.php? Will that be fine? I’m referring to the security of the data.

  8. @Robert is right we need to put ‘contact_id’=>’ID’,
    in config.php array. Tnx Jessica

Add your comment now