• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Lesson 6. Modifying Databases > Deleting Information from a Database

Deleting Information from a Database

There will be times when you need to remove information from a database. The information could be outdated, a product may be sold out—whatever the reason, you still need to delete it. And deleting, like inserting or updating, is simple with ColdFusion.

Continuing with productmenu.cfm, add the following link directly after the initial link for updating:

<a href=”productdelete.cfm?productid=#productid#”>Delete</a> 

You have now created a link that will be inserted after every product's name that, when clicked, will delete it from the database. When this link is clicked, the product's entire record will be removed from the database . Once again, you've had to pass the product ID to the template working with the database in order for it to know what to delete.

Choose File > New and select Blank Document from the New Document window.

This blank document is where the query to actually delete the information will take place. This query, like the others, will not contain any HTML.

Add the first line of the query:

<cfquery name=”productdelete” datasource=”products”> 

As usual, you must specify the <cfquery> tag with a name and a data source to work with.

Add the following SQL:

from tblProducts

By using the Delete keyword you are deleting all the information from the table you specify, (in this case the tblProducts table). Be very careful when using the Delete keyword—one misstep and you can lose all the data in your table!

Add the clause where productid = #url.productid# to your Delete statement.

Just like the Update statement, the Delete statement needs to know which specific customer to work with. If this where statement were not present, all the records in the table would be deleted.

Add the closing </cfquery> tag.

Your Delete statement is now complete and functional. The last thing to do, as with the other processing pages, is to send the user back to another page after the processing occurs. In this case, it makes the most sense to return the user to the productmenu.cfm template, where they will be able to verify that the customer was indeed deleted.

Add the following CFML after your query:

<cflocation url=”productmenu.cfm”> 

Once again, you are sending the user back to the productmenu.cfm template after the query has finished processing.

Choose File > Save and save the file as productdelete.cfm in the Lesson6/Start folder on your local hard drive.

Now that the file is saved, you can test it out to make sure that it works. But remember that if you do delete products, the records are really gone, and if you wanted them in the database again, you would have to add them back manually.

Open a browser and type into the address field.

This will once again bring up your productmenu.cfm template, which now includes the Delete link. When you click this link, the product associated with that link will be removed from the database.

Click Delete next to one of the names.

This will remove the product to the left of the link from the database and the productmenu.cfm will reload. In our example, notice that the leather case product is no longer on the list.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint