Adding the Delete Functionality

Writing the script for the delete processor page is even easier than writing the script for the update processor. Remember, users will access this page from the master page, which you have already built, so all that remains is to add the script that deletes the record from the database.

  1. Open admin_cp_delete_processor.asp in code view, and remove any code.

    Once again, this page will contain only a short script and a redirect elsewhere, so it needs no HTML code at all.

  2. Add the code that creates the connection.

    In ASP:

    <%
    Dim dbConn
    set dbConn = server.CreateObject("adodb.connection")
    dbConn.open("newland")
    
    %>
    

    In ColdFusion:

    <cfquery name="delete_country" datasource="newland">
    
    </cfquery>
    

    Now you can insert the code that executes a SQL statement.

  3. Add the code that deletes the record specified in the URL parameter, in the space you left in the preceding step.

    In ASP (all in one line):

    [View full width]
    dbConn.Execute("DELETE FROM tbl_country WHERE countryID=" & Request.QueryString( graphics/ccc.gif"countryID"))

    In ColdFusion (may be on multiple lines):

    DELETE FROM tbl_country
    WHERE countryID=#URL.countryID#
    

    With SQL's DELETE statement, you don't have to specify each field singly, because it deletes from all fields, making it much more convenient to write. It also makes it easy to inadvertently wipe out your entire table, if you forget to specify the WHERE clause!

  4. Add the redirection back to the master page, in the appropriate location.

    In ASP, in the line above the closing %> tag:

    Response.Redirect("admin_cp_master.asp")
    

    In ColdFusion, in the line after the closing </cfquery> tag:

    <cflocation url="admin_cp_master.cfm">
    

    This returns the user to the master page, so she or he can conveniently verify that the deletion took place.

    graphics/16inf22.gif

  5. Save and upload the file. Press F12 to open admin_cp_master.asp in a browser, and delete the country you inserted earlier in the lesson.

    As promised, the country disappears instantly and permanentlydon't bother trying the Back button!

    graphics/16fig14.jpg