Deleting records from your database.

This tutorial will demonstrate how to delete records from the database. 

IMPORTANT NOTE: The first thing you must understand is that delete is final (in other words, there's no going back so backup your database before doing anything!)

OK, deleting data from your database is really a very simple procedure, let's start a step before that which is on the page where you list your records. (We'll call it list.cfm)

<!--- LIST.CFM --->
<!--- first, let's get our records to display to the user --->

<cfquery name="qMyQuery" datasource="MyDSN">
    SELECT         ID, TITLE, DESCRIPTION
    FROM           TableName
    ORDER BY     Title
</cfquery>

<!--- Now display the records to the user --->
<cfoutput query="qMyQuery">
    #title# - #description#<BR>
     <a href="delete.cfm?id=#id#" onClick="return confirm('Are You Sure You Want To Delete This Record?');">Delete</a>
    <HR>
</cfquery>

<!--- Now put an option to delete all records at once --->
<a href="delete.cfm" onClick="return confirm('Are You Sure You Want To Delete ALL Records?');"
>Delete All Records</a>

Notice you passed a value called ID (This is the ID that makes this record different), that also created a hyperlink to a page called "delete" this is where the delete will take place. Let's take a look at the delete.cfm page now... Also note that there is an OnClick value, this will confirm that you really do want to delete it, in case you click on a wrong link! :)

<!--- DELETE.CFM --->
<!--- This is the query that will delete the data --->
<cfquery name="qDelete" datasource="MyDSN">
    DELETE FROM TableName
   
WHERE ID = #id#
</cfquery>

Ok, now let me explain the code a bit...
the code is basically a query that deletes a record/row from a table where the ID is the ID that was passed from the LIST page. If you wanted to delete all records in a table, then simply remove the WHERE code...

It's that simple, but remember (actually deleting data is not a good thing...) as an alternative to deleting data, I suggest you add a field to the database table called "Active" (BIT or Yes/No Format) or something like that... then if the user wants to delete, simply set that value to 0 for delete, and 1 for active or available. By not deleting you can always access it at a later time, if you delete a value... then it's gone forever.....

Questions? Comments? Email me...

About This Tutorial
Author: Pablo Varando
Skill Level: Beginner 
 
 
 
Platforms Tested: CF4,CF5,CFMX,BlueDragon
Total Views: 73,712
Submission Date: July 04, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial
  • There is delete option in list of my application and after hitting the delete button a pop up box will come ask for the confirmation...YES or NO ...if yes is clicked ...the record should get deleted if NO is clicked we should get out of pop up box....Now the issue is after clicking YES the record gets deleted but the next page is coming in the pop up only....I am not getting out of pop up box ...I know it is very easy ..But now it is out of my scope...please help...Thanks in advance

  • ok the first part of your code works but when it goes to the next page i get an error message which says id is undefined.

  • I implant this code in my site.. but i have liked databases in MS access an tell me, "Erase first the records linked"... any way to delete all records what have this records liked? a example.. I have 3 tables the first table have the Name of Artist.. the second have the albums form Artists and this table have relationship whin the before table and the third table have the track list, images, and genere.. And this too have relationship whit the firs and second table... any way to delete all records what i have by artist or album?

  • Thanks for this it helps loads!... i have a little query though, ive managed to get this to work for my gallery which removes the data of the image from the database but how would i remove the image from the folder that it gets posted to as well? Praksha

  • Manish, Thanks for the great words about the tutorial... as far as certifications, the best place to start is: http://www.macromedia.com/support/training/ Thanks again, Pablo Varando

  • Really Excellent . After a long time i found this excellent tutorial .Really keep it up .i am impressed . I am thinking for certification in Coldfusion Mx .Where should i get free sample paper for exams .really this will be a great help . Regards Manish

Advertisement

Sponsored By...
Powered By...