Contents > Developing ColdFusion MX Applications > Updating Your Database > Deleting data > Deleting a single record PreviousNext

Deleting a single record

To delete a single record, use the table's primary key in the WHERE condition of a SQL DELETE statement. In the following procedure, Emp_ID is the primary key, so the SQL Delete statement is as follows:

DELETE FROM Employee WHERE Emp_ID = #Form.Emp_ID#

You often want to see the data before you delete it. The following procedure displays the data to be deleted by reusing the form page used to insert and update data. Any data that you enter in the form before submitting it is not used, so you can use a table to display the record to be deleted instead.

To delete one record from a database:

  1. In update_form.cfm, change the title to "Delete Form" and the text on the submit button to "Delete Record".
  2. Change the form tag so that it appears as follows:
    <form action="delete_action.cfm" method="Post">
    
  3. Save the modified file as delete_form.cfm.
  4. Create a ColdFusion page with the following content:
    <html>
    <head>
    <title>Delete Employee Record</title>
    </head>
    <body>
    
    <cfquery name="DeleteEmployee"
       datasource="CompanyInfo">
          DELETE FROM Employee
          WHERE Emp_ID = #Form.Emp_ID#
    </cfquery>
    
    <h1>The employee record has been deleted.</h1>
    <cfoutput>
    You have deleted #Form.FirstName# #Form.LastName# from the 
    employee database. </cfoutput> </body> </html>
  5. Save the page as delete_action.cfm.
  6. View delete_form.cfm in your web browser by specifying the page URL and an Employee ID; for example, enter the following:

    http://localhost/myapps/delete_form.cfm?Emp_ID=3.Click Delete Record.

    ColdFusion deletes the record in the Employee table and displays a confirmation message.

Reviewing the code

The following table describes the code and its function:

Code

Description

<cfquery name="DeleteEmployee"
   datasource="CompanyInfo">
      DELETE FROM Employee
      WHERE Emp_ID = #Form.Emp_ID#
</cfquery>

Deletes the record in the database whose Emp_ID column matches the Emp_ID (hidden) field on the form. Since the Emp_ID is the table's primary key, only one record is deleted.

<cfoutput>
You have deleted #Form.FirstName#
   #Form.LastName# from the
   employee database.
</cfoutput>

Informs the user that the record was deleted.


Contents > Developing ColdFusion MX Applications > Updating Your Database > Deleting data > Deleting a single record PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.