Section 22.4. Deleting Records

22.4. Deleting Records

Dreamweaver's Delete Record server behavior lets you build pages for removing records from a database. Depending on which server model you use, the method of adding this server behavior varies. The Dreamweaver server behavior for ASP and JSP is the same, while the server behavior for PHP, ASP.NET, and ColdFusion offers a bit more flexibility.

22.4.1. Deleting Records for ASP and JSP

The setup is similar to using the Update Record server behavior, in that you must create a page with a recordset that retrieves a single recordthe item to delete. This is just like a detail page, as described on Section 21.6. Another page must link to the delete page and provide the proper key for filtering to a single record on the delete page.

One way to go about this is to add a linkDelete This Record, perhapson a record detail page. The link should pass the primary key for the condemned record to another page (the Go to Detail Page server behavior described on Section 21.7 can help with this). This pagethe delete pagewould include a recordset that retrieves the file to be deleted. To confirm the deletion, the page might say something like, "Are you sure you wish to delete this record?" (Adding some dynamic text from the recordset, such as a name column, will help identify the record.)

What makes a delete page differ from a regular detail page is that it also includes a form with a single Submit button and a Delete Record server behavior.

To create a delete page, proceed as follows :

  1. Create a dynamic page containing a filtered recordset .

    As noted above, the recordset should retrieve a single recordthe one to be deleted. However, you don't need to retrieve all of the columns for the record. At a minimum, the recordset must retrieve the record's primary key, since the Delete Record server behavior needs it to know which record to remove. But beyond that, you should probably include some identifying information on the delete page, so that your visitor can make sure she's really deleting the right record. For example, if the page deletes an employee from the database, consider putting the employee's name on the page as one final check.

  2. Add a form to the page with a single Submit button (see Section 10.3.8) .

    Change the label of the button so that it reflects the action it will performDelete This Record, for example.

  3. Open the Server Behaviors panel (Window Server Behaviors). Click the + button and select Delete Record .

    The Delete Record window appears (see Figure 22-13).

    Figure 22-13. Dreamweaver's Delete Record server behavior simplifies the process of removing information from a database.
  4. Select a database from the Connection menu. From the "Delete from table" menu, select the table with the record to be deleted .

    These steps should be familiar by now; the table you select should be the same one you used when creating the detail recordset for this page.

  5. From the "Select record from" menu, select the recordset you added to this page .

    This specifies the recordset on the page (if there's more than one) that Dreamweaver should use for specifying the record to be deleted.

  6. Choose the table's primary key from the "Unique key column" menu. If it's a number, make sure the Numeric box is also turned on .

    The "Unique key column" identifies which record to delete. (For a description of primary keys, see Section 20.3.2.) Make sure you retrieved this primary-key information when you created the recordset, or the server behavior won't work.

  7. Select the name of the form that contains the Delete button .

    Unless the page has more than one form, the name of the form you created in step 2 should appear here automatically.

  8. Click the Browse button; navigate to and select a file from the Web site .

    This represents the page that will appear after a record has been deleted. This could either be a page with a confirmation message"The Record has been successfully deleted"or a page that lists the records of the databasea master page.

  9. Click the OK button .

    Dreamweaver inserts the code necessary to delete a record from the database.

22.4.2. Deleting Records for PHP, ASP.NET, and ColdFusion

Deleting a record using ASP or JSP is straightforward. For the other server models, the Delete Record server behavior offers a bit more flexibility and can be implemented in a variety of ways. You do not have to add a recordset to a page to delete a record, nor do you have to add a form with a delete button.

The main requirement: the page with the Delete Record server behavior must have some way to retrieve the primary key for the record you wish to delete. This can be a form, a URL, a cookie, a session variable, or any of the other data sources discussed on Section 23.2.1.

For example, on a page that lists the details of a particular record, you could include a link to a delete page and pass the ID of that record in the URL (see "Passing Information Between Pages" on Section 21.7). The delete page could then use the ID number in the URL to delete the record and then send the visitor off to another pageperhaps a page verifying that the record was successfully deleted.


Note: Because of this flexibility, under the PHP, ASP.NET, and ColdFusion server models, you could place a Delete Record server behavior on a blank dynamic page. All the page would do is delete the specified record and then go to another page on the site.

There are many ways, therefore, that you could delete a record in this server model. Here is a method that would provide the same experience as the ASP and JSP models discussed abovethat is, a delete page that lets people confirm the item they wish to delete by clicking a button on a form:

  1. On one of the pages in your site, add a link to the delete page .

    For example, on a page that provides the details of a single record, you could add a link to the delete pagemaybe the word "Delete" or a button with a picture of a trash can.

    Alternatively, you could add a "delete this record" link as part of a repeating region (see Section 21.3.2). In this way, you would have multiple records on a single page, each with its own link to the delete page. In both cases, you'd attach the record's primary key to the link, as described on Section 21.7.

  2. Create a dynamic pagethe delete pagecontaining a filtered recordset .

    This recordset should retrieve a single recordthe one to be deleted. As mentioned for the ASP and JSP models, you don't need to retrieve all of the columns for the record. At a minimum, the recordset must retrieve the record's primary key, since the Delete Record server behavior needs it to know which record to remove. You may want to include some identifying information, such as the name of the item to be deleted, so that your visitors can see what they're about to delete.

  3. Add to the page a form consisting of a single Submit button (as described on Section 10.3.8) .

    When you create the button, change its label to reflect what it doesDelete This Record, for example.

  4. Select the form. Then, in the Action box in the Property inspector, type the page's file name .

    For example, if the page is called delete.php , type delete.php .

    The Action property indicates where the form should be sent (see Section 10.2). In this case, when the visitor clicks the Submit button, the form information goes back to the same page.

    This kind of trickery is common in dynamic pages. When your visitor clicks the Delete button, the form is sent to the same pagebut this time the form doesn't show up. Instead, the Delete Record server behavior (which you're going to add in step 6) deletes the record and sends the visitor off to another page.

  5. Add a hidden field to the form (Section 10.3.7). Name this field whatever you wish, but bind (attach) to it the primary key from the recordset you created in step 2 .

    This hidden field is what tells the Delete Record server behavior which record to delete. (For instructions on binding dynamic data to a form field, see Section 22.3.)

  6. Open the Server Behaviors panel (Window Server Behaviors). Click the + button and select Delete Record from the list of server behaviors .

    The Delete Record window appears (Figure 22-14). This window differs slightly among ASP.NET, PHP, and ColdFusion. However, the basic steps described here are the same.

    Figure 22-14. The Delete Record window for PHP differs slightly from its .NET and ColdFusion counterparts. .NET lets you specify an error page so that visitors will see a different page if the delete action fails; ColdFusion lets you specify a user name and password for the database.
  7. From the "First check if variable is defined" menu, choose Primary Key Value .

    This may seem like putting the cart before the horse, because you won't define the primary key value until step 10 below. However, this option lets you control when the record is deleted, by making sure the proper variable is defined before the server behavior deletes the record. In this case, the record won't go away until the visitor clicks the Delete button, causing the server to send a form variable containing the record's primary key.

    This is a necessary precaution; without this option, the record would be deleted whenever the page loads. Since the page really serves two functionsallowing visitors to confirm that they wish to delete the record, and actually deleting the record from the databaseyou need to make sure the visitor has first visited the page and then clicked the Submit button you added in step 3.

  8. From the Connection menu, select a database. From the Table menu, select the table with the record to be deleted .

    These steps should be familiar by now. The table you select should be the same one you used when creating the detail recordset for this page.

  9. From the "Primary key column" menu, select the table's primary key .

    This tells Dreamweaver which database field contains the unique key that identifies which record to zap from the table.

  10. From the "Primary key value" menu, select Form Variable. In the box just to the right of that menu, type the name of the hidden field you added in step 5 .

    This is the final piece of the puzzle. It tells the server behavior where to find the ID number for the doomed record. In this case, the form with its hidden field will supply the ID number.

  11. Click the Browse button; navigate to and select your confirmation page from the Web site .

    The confirmation page will appear after a record has been deleted. It's a page you've created in advanceeither a page with a confirmation message ("The Record has been successfully deleted"), or a page that lists the records of the databasea master page.

  12. Click OK .

    Dreamweaver inserts the code necessary to remove a record from the database.

As with any of the other server behaviors that change content in a database, you should carefully control who has access to the delete page. Going to work one morning and finding that someone has deleted all the products from your company's e-commerce site is enough to ruin your whole morning. Dreamweaver's User Authentication behaviors, discussed in the next chapter, can help.

If you ever need to change any of the properties of the Record Delete action, such as picking a different page to go to after a record is deleted, you can double-click Delete Record on the Server Behaviors panel. The Delete Record window opens; make any changes and click OK.



Dreamweaver 8[c] The Missing Manual
Dreamweaver 8[c] The Missing Manual
ISBN: 596100566
EAN: N/A
Year: 2006
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net