Database Operations

     

If you want to use ColdFusion to integrate data into your Web pages, but you're not an expert in databases, CFML, or Structured Query Language (SQL), Dreamweaver can make your life a lot easier.

NOTE

Structured Query Language (SQL, usually pronounced SEE'-KWUL) is the standard programming language for interacting with databases.


Specifically , here are three important tasks that Dreamweaver automates, often so successfully that they become almost instantaneous:

  • Databases organize data into tables, and within tables into fields or columns . This organization is the schema of the database. Dreamweaver displays the schema in a hierarchical tree format that makes it easier to see how the data is organized. Dreamweaver also allows you to preview the data itself.

  • CFML extends HTML with a set of tags, the most important of which are database- related . For four basic operations ”retrieving, updating, inserting, and deleting data ”Dreamweaver automatically produces the CFML code. The CFML code, which you can see in Code View mode in the document window, is a database command enclosed in CFML tags ”specifically, <cfquery> </cfquery> tags.

  • The database command inside the <cfquery> tags is expressed as a SQL statement, which the ColdFusion server passes on to the database, which performs the desired operation. The <cfquery> tag is similar for all these operations, but the SQL commands and syntax change depending on the operation. Dreamweaver produces the SQL statement automatically, so you don't have to know any SQL.

You can modify, delete, or move the CFML and SQL code produced by Dreamweaver. Do this in Code View mode in the document window.

For more on CFML and ColdFusion architecture, see Chapter 32, page 875 , and Chapter 33, "ColdFusion Markup Language (CFML)," page 885 .


Previewing Data

Before you can do any of the things described in the rest of this chapter, ColdFusion must be up and running. See Chapter 32 for a discussion of what this may entail, beyond just installing ColdFusion.

Dreamweaver offers a quick, convenient way to look at your data. You might want to do this, for example, to get an idea what the data will look like when you display it. Data is displayed 25 rows (records) at a time. Here's the procedure for previewing data:

  1. Go to the Databases panel in the Application panel group . Here you'll find a listing of all the databases for which a system DSN has been defined on your machine.

    The databases are grayed out and inaccessible until you open a document. For previewing data, any kind of Dreamweaver document will do. The document is not used for previewing the data. A ColdFusion document is used in the next section on retrieving data. To open a ColdFusion document, click on ColdFusion in the Create New column of the startup page, or select File, New, Dynamic page, ColdFusion.

  2. Click the plus sign to the left of the database name to expand the first level of the tree displaying the database's schema (structure). (Also loads the schema if it's not already loaded.)

  3. Click the plus sign to the left of Tables to see a list of tables. (Figure 13.2 shows the greendept database expanded to show the fields in the Products table.)

    Figure 13.2. The Databases panel in the Application panel group.
    graphics/13fig02.jpg

  4. Select the table you want to preview, bring up the context menu (right-click on the PC or Control-click on the Mac) and choose View Data, as shown in Figure 13.3. Figure 13.4 shows the preview of the Categories table in the greendept database.

    Figure 13.3. The context menu (right-click on the PC, Control-click on the Mac) options for a database table.

    graphics/13fig03.jpg


    Figure 13.4. The View data screen as the Categories table was being built in the greendept database. To get this kind of display, select the table, right-click (PC) or Control-click (Mac) and choose View Data, as shown in Figure 13.3.

    graphics/13fig04.jpg


NOTE

Where the PC uses plus signs and minus signs to expand and collapse hierarchical trees, the Mac uses right-facing triangles and downward facing triangles, similar to those for maximizing and minimizing panels.


Click the plus or minus signs to expand or collapse the tree. Clicking the plus sign to the left of a database (such as greendept) both loads the database schema and expands the tree. The Refresh button reloads the database schema, so any changes you've made will show up. The Modify data sources button takes you to the ColdFusion Administrator, where you can manage system DSNs.

Choose View Data to browse the data in the table. Other options are

  • Modify Data Sources (grayed out because it is active only for databases, not database tables) takes you to the ColdFusion Administrator, where you can add, delete, or modify system DSNs.

  • Test Connection is grayed out for ColdFusion. For other server models, it tests the connection to the database. Not necessary for ColdFusion.

  • Insert Code, active only in Code View, inserts the name of the selected database, table, or field into the code.

  • Refresh reloads the database schema, so that the Databases panel reflects any changes made since the last load.

graphics/troubleshooting_icon.jpg

Getting a timeout error when trying to view data? There are several possible solutions in the "Troubleshooting" section at the end of this chapter.


If, after loading the database schema (step 2 in the procedure just described), you change something in the schema ”add or delete a table, for instance, or change the fields in a table ”you will need to refresh the Databases panel to have those changes show up there. You can do this in one of two ways: by using the Refresh button in the upper right of the Databases panel (see Figure 13.2), or by selecting Refresh in the context menu for the database, a table, or a field.

Retrieving Data

Unlike previewing data, in which you do not use a Dreamweaver document in any way, you retrieve data by coding and running a ColdFusion document. So if you do not have a ColdFusion document open yet, click ColdFusion in the Create New column of the startup page, or select File, New, Dynamic page, ColdFusion. If you were coding by hand, you would retrieve data by enclosing an SQL SELECT statement within a ColdFusion <cfquery> tag. Instead, you can use the Dreamweaver Bindings panel in the Application panel group to create the SQL and CFML code automatically.

NOTE

You can create CFML by dragging and dropping from the Bindings panel into the document window. For instance, in Figure 13.8, dragging ProductID into the document creates this CFML code: <cfoutput>#qProducts.ProductID#</cfoutput> . This displays the ProductID of the first record in the Recordset.

Figure 13.8. The completed Recordset, in the document window on the left (within the <cfquery> tags), and represented visually in the Bindings panel on the right. Click the Refresh button in the upper right corner to update the Bindings panel if you add, edit, or delete a Recordset in the document window.
graphics/13fig08.jpg


NOTE

You can drag and drop from the Databases panel into the document window, too. In Code View, the name of the table or column is created in the document, which helps you avoid typos. In Design View, dragging a table brings up the Recordset dialog configured for that table.


Here's the procedure for retrieving data:

  1. Go to the Bindings panel in the Application panel group. If you have already created one or more Recordsets, they're listed here. If you haven't created any Recordsets yet, you see the list of instructions shown in Figure 13.5, the final instruction being to press the + button and choose Recordset.

    Figure 13.5. This is what you'll see in the Bindings panel in the Application panel group prior to creating any Recordsets.

    graphics/13fig05.jpg


  2. Press the + button and choose Recordset, as shown in Figure 13.6. This brings up the Recordset dialog, shown in Figure 13.7.

    Figure 13.6. Press the + button and choose Recordset to bring up the Recordset dialog, which automates the process of creating CFML and SQL code for retrieving data.

    graphics/13fig06.jpg


    Figure 13.7. The Recordset dialog, which automates the process of creating CFML and SQL code for retrieving data.

    graphics/13fig07.jpg


  3. Fill in the Recordset dialog, as shown in Figure 13.7. Provide an arbitrary name for the query that you are creating ( qProducts in the figure). Choose the appropriate system DSN from the Data source drop-down menu (greendept in the figure). If the database is password-protected, provide the username and password (not required in this example). Select the columns that you want to retrieve. Optionally , select a field and a value on which to filter. In the figure, the only records selected will be those for which the Category field is equal to 13001010000 (the category code for books). Without this filter, all products would be retrieved; with it, only books are retrieved.

  4. Click the OK button in the upper-right corner of the Recordset dialog. The Recordset is created in the document, as shown in the document window (on the left) in Figure 13.8. The Recordset is also represented visually in the Bindings panel, as shown on the right in Figure 13.8.

If you want to edit a Recordset, you have two options: If you are comfortable with the CFML and SQL involved, you can edit the code manually in Code View in the document window. Alternatively, you can double click any of the text associated with the Recordset in the Bindings panel. You have to click on text, not an icon. For example, in Figure 13.8, you would click on Recordset (qProducts) or any of the field names below that ( ProductID and so on). This brings up the Recordset dialog (Figure 13.7), allowing you to edit the Recordset.

Note that all the columns in the Products table have been selected in this example. Clicking the All radio button in the Recordset dialog could have accomplished this. (Refer to Figure 13.7.) Instead, the Select radio button was clicked and then all the columns were selected. If the All radio button had been clicked, the SQL statement in Figure 13.8 would have begun SELECT * FROM Products . The asterisk is a wild-card meaning "all columns." This works fine in Microsoft Access. However, some other databases may automatically create other columns that you do not want to retrieve. If you were to use your code with those databases, you would retrieve the unwanted columns. Therefore, to make your code more flexible and portable across database systems, it is better to list each column individually.

Notice that the Bindings panel has a Refresh button in the upper-right corner. Click this button to update the view in the Bindings panel after you modify a Recordset.

If you load the page just created ( qProducts.cfm in Figure 13.8, also available on the CD accompanying this book) into a browser, nothing is displayed. By itself, a Recordset displays nothing. It simply retrieves and holds data for possible use elsewhere in the document. The next section shows various ways to use the Recordset data in the document.

By default, the Recordset data is available only in the document that contains the Recordset. To access the Recordset from other documents, you can store the Recordset in a ColdFusion variable that persists across documents, such as a client, session, or application variable. Another possibility is to pass individual pieces of data from page to page as URL variables .

NOTE

Dreamweaver enables you to create development-time variables for pages that need a form variable, URL variable, or other variable. While in the dynamic page, click + on the Bindings panel and choose the variable type. Variables created here exist only in Dreamweaver.


Using Dynamic Data

After you have created a Recordset, you can either display the data in the browser or use it "behind the scenes" to determine whether a check box is checked, for instance, or whether an item on a selection list is selected.

Dreamweaver makes it easy for you to insert data anywhere in your Web page as text, in a table, or as an item on a menu or selection list. You can also use retrieved data to determine whether a check box is checked, or whether a radio button or list item is selected.

In addition, there is a Live Data feature that enables you to see the data in Dreamweaver while editing the page in Design View.

With the exception of the Live Data feature, the features illustrated in this section are all data insertion features. You get to them via the Insert menu on the main menu bar, or via the Insert toolbar. The Insert toolbar is generally quicker. Here, we'll look at six features reached via the Dynamic Data button on the Insert bar, shown in Figure 13.9. This is a multi-purpose button: Click the downward-facing triangle on the right side of the button to display these six options, then select the option you want. The button remains associated with that option until you change it, even if you the exit and restart Dreamweaver.

Figure 13.9. The Dynamic Data button on the Insert bar.

graphics/13fig09.jpg


Creating and Editing a Dynamic Table

The first option, Dynamic Table, uses an HTML table, combined with a little CFML, to display either all the data in the Recordset, or a configurable number of records starting from the beginning of the Recordset. It also allows you to set three standard HTML table attributes (border, cell padding, and cell spacing), as shown in Figure 13.10. After you've created the table, you can edit it in Code View or Design View.

Figure 13.10. The Dynamic Table dialog. As configured here, the dynamic table will show the first 10 records retrieved. For navigation to subsequent records, add a Recordset Navigation Bar, as shown in Figure 13.11.

graphics/13fig10.jpg


Figure 13.11 shows a portion of the dynamic table displaying the qProducts Recordset.

Figure 13.11. A portion of the dynamic table configured in Figure 13.10, displayed in a browser. Above the table, the Recordset Paging button on the Insert bar (shown in Figure 13.12) was used to add a Recordset Navigation Bar.
graphics/13fig11.jpg

A dynamic table by itself does not include any navigation. However, you can easily add navigation by using the Recordset Paging button on the Insert bar, shown in figure 13.12. This is the procedure:

  1. Click at the point in the document where you want to insert the Recordset navigation control.

  2. Click on the Recordset Paging button on the Insert bar, bringing up the Recordset Navigation Bar dialog. Both the button and the dialog are shown in Figure 13.12.

  3. On the Recordset Navigation Bar dialog, choose a Recordset and select either text or images, to show the navigation controls as text or VCR-style visual controls. The latter are illustrated at the top of Figure 13.11.

  4. Click the OK button.

Figure 13.12. The Recordset Paging button, and the Recordset Navigation Bar dialog it brings up, enable you to add navigation for a dynamic table.

graphics/13fig12.jpg


After you've created the dynamic table, you can edit it in Code View. For instance, if there are columns that you don't need, it's easy to eliminate them. In Figure 13.10, for example, ProductID, Category, and ProductURLID would not be meaningful to a user . (In the database, they are used as links to other tables that do provide meaningful information.) It's easy to eliminate them.

A Dreamweaver dynamic table is an HTML table with two rows. The first row is pure HTML. It contains the column headings, taken from the field names in the database. The second row contains ColdFusion variables, enclosed in pound signs, like this:

#qProducts.ProductID# . (In ColdFusion, pound signs indicate a variable as opposed to ordinary, literal text.) The second row also has CFML cfoutput tags around it, to display the data.

In the following HTML/CFML listing of the qProducts table, you'd delete the six bolded lines to eliminate the three columns that are not useful to the end user.

 <table border="1">   <tr>  <td>ProductID</td>   <td>Category</td>  <td>ProductName</td>     <td>Description</td>  <td>ProductURLID</td>  <td>ListPrice</td>     <td>Weight</td>     <td>ProductSize</td>     <td>ProductApplication</td>     <td>AlternativeTo</td>     <td>Brand</td>     <td>SmallImageName</td>     <td>MediumImageName</td>     <td>LargeImageName</td>     <td>WhenEntered</td>     <td>Owner</td>   </tr>   <cfoutput query="qProducts"      startRow="#StartRow_qProducts#" maxRows="#MaxRows_qProducts#">     <tr>  <td>#qProducts.ProductID#</td>   <td>#qProducts.Category#</td>  <td>#qProducts.ProductName#</td>       <td>#qProducts.Description#</td>  <td>#qProducts.ProductURLID#</td>  <td>#qProducts.ListPrice#</td>       <td>#qProducts.Weight#</td>       <td>#qProducts.ProductSize#</td>       <td>#qProducts.ProductApplication#</td>       <td>#qProducts.AlternativeTo#</td>       <td>#qProducts.Brand#</td>       <td>#qProducts.SmallImageName#</td>       <td>#qProducts.MediumImageName#</td>       <td>#qProducts.LargeImageName#</td>       <td>#qProducts.WhenEntered#</td>       <td>#qProducts.Owner#</td>     </tr>   </cfoutput> </table> 

At the same time, you could "clean up" some of the other headings. For instance, ListPrice would be better as List Price.

If you try this exercise with greendept.mdb from the CD, you'll see that other headings look just plain wrong: For instance, the Weight heading has peoples' names under it, and the text in the ProductSize column clearly has nothing to do with size . This database is designed to hold any type of product, service or event. Rather than try to design a single set of fields to fit all the possible categories, it was decided to re-purpose fields as needed for each category. For books, for instance, we don't need to know the weight, but we do need to know the author. Therefore, the Weight field was re-purposed to hold the author's name.

Although there are ways to use CFML to put in correct headings, you can just manually edit the headings here. Because this Recordset contains only books, change Weight to Author, and ProductSize to Subject in the first row. Make changes only in the first row. ColdFusion will still think of the author's name as #qProducts.Weight# , for example, so nothing in the second row should change.

On the CD accompanying this book, qProducts2.cfm has the unaltered version of the qProducts dynamic table, whereas qProducts2b.cfm has an edited version. (If you're curious , the ProductFieldLabels table has headings for all categories and would form the basis for automating the process of applying the right headings.)

Inserting Dynamic Text

Dynamic text is most commonly used when a Recordset is known to contain only one row. If the Recordset contains more than one row of data, as in the case of the qProducts Recordset, the dynamic text by itself displays data from the first row only. One way to display data from multiple rows is to use a dynamic table.

Figure 13.13 shows the Dynamic Text configuration dialog, the resulting CFML, and a typical result in the browser. On the configuration dialog, select a field name and optionally apply a format. Dreamweaver automatically creates the code in the Code box. You can also edit the code in the Code box, before clicking OK. After you've created the dynamic text, you can edit it in Code View or Design View.

Figure 13.13. Inserting dynamic text: the configuration dialog, the resulting CFML, and a typical result in the browser.

graphics/13fig13.jpg


Using Dynamic Data in Forms and Menus

Using dynamic data in forms and menus can simplify site maintenance. For instance, if a group of menu options appears on many pages, you can change the options on all those pages by editing the data in one place: in the database. The same principle holds for radio buttons , check boxes, text fields, and images.

Two common to ways of using Recordset data in a form are displaying the data and using it to determine the state of a visual control. Dreamweaver auto-generates code for both of these uses. The data can be displayed in a text box or selection list. In addition, it can be used to decide whether a check box should be checked by default, or whether a radio button, menu item, or list item should be selected by default.

Before adding dynamic data functionality, you must create a form. Select the Forms category by using the menu at the left of the Insert bar. Create the form with the Form button and add other form elements as desired by using the buttons on the Insert bar, shown in Figure 13.14. The left-most eight form elements on the Insert bar can use dynamic data, either displaying the data (Text Field and Text Area) or containing the data without showing it (Hidden Field), or they can use the data to determine the state of the form control (Check Box, Radio, and Radio Group). List/Menu and Jump Menu can both display data and use data to determine which item or items in the list or menu are selected initially.

Figure 13.14. Use the Forms category on the Insert bar to create a form. Any of the first eight form elements on the Insert bar works with dynamic data. After creating the form, go to the Application category to use dynamic data with the form.
graphics/13fig14.jpg

The basic procedure for using dynamic data in forms is the same for all types of form elements: After creating the form, go to the Application category and select one of the bottom four Dynamic Data button options, shown earlier in Figure 13.9. Each of the four works only with particular form elements. For example, Dynamic Text works with Text Field, Hidden Field, and Text Area. Dynamic Checkbox works with the Checkbox form element. Dynamic Radio Group works with Radio and Radio Group. List/Menu works with the List/Menu and Jump Menu form elements.

Dreamweaver looks at all the forms in your document, determines which of the form elements in your document can be used with the dynamic data feature you have chosen , and presents you with a configuration dialog listing those elements so that you can choose one. The configuration dialog also lets you specify from which Recordset field to get the data.

The dynamic Text Field just displays the data in the database Field that you select. (See Figure 13.15.) The auto-generated code looks like this:

 <input value="<cfoutput>#qProducts.ProductName#</cfoutput>"       name="productName" type="text"> 

Figure 13.15. The Dynamic Text dialog, which enables you display dynamic data in a text field. (An example of auto-generated code is shown in Figure 13.17.)
graphics/13fig15.jpg

This code uses the ColdFusion cfoutput tag to display the dynamic data. (All the sample code in this section is in qProducts_form.cfm , on the CD accompanying this book.)

The dynamic check box is checked if the dynamic data that you select is equal to a value that you enter in the dialog. (See Figure 13.16.)

Figure 13.16. The Dynamic Checkbox dialog, which enables you to use dynamic data to determine whether a check box is checked.
graphics/13fig16.jpg

Here's the code Dreamweaver auto-generated based on the dialog shown in Figure 13.16.

 <input <cfif (#qProducts.Category# EQ 13001010000)>checked</cfif>       name="isBook" type="checkbox" value=""> 

This code uses the ColdFusion cfif tag to include the checked attribute only if the stated condition is met, namely that the dynamic data, qProducts.Category , is equal to 13001010000.

Radio buttons are checked if the dynamic data that you select is equal to the value assigned to the radio button in the original form. (See Figure 13.17.)

Figure 13.17. The Dynamic Radio Group dialog, which allows you to use dynamic data to determine the checked/unchecked state of a radio button or buttons.
graphics/13fig17.jpg

Here's code for one radio button, auto-generated by Dreamweaver based on the dialog shown in Figure 13.17:

 <input <cfoutput>#Iif(qProducts.Category EQ "14005000000",DE("CHECKED"),DE(""))#      </cfoutput> type="radio" name="Shipping" value="14005000000"> 

This code uses the ColdFusion iif tag to include the CHECKED attribute only if the stated condition is met, namely that the dynamic data, qProducts.Category , is equal to 14005000000, the value assigned to the radio button in the original HTML-only form:

 <input type="radio" name="Shipping" value="14005000000"> 

When you create a dynamic list or menu, you specify (from top to bottom on the dialog shown in Figure 13.18):

  1. The list or menu.

  2. Optional static values that will be submitted to the server when the form is submitted, and display labels to accompany those static values.

  3. The Recordset to use for dynamic data.

  4. A dynamic data item that determines the value returned to the server when the form is submitted.

  5. A dynamic data item for generating display labels.

  6. A comparison value, which can be either entered manually or generated dynamically. If the value in step 4 equals the comparison value, the list or menu item will be pre-selected when the page is initially loaded.

Figure 13.18. The List/Menu dialog enables you display dynamic data in a list or menu and determine which items are pre-selected.
graphics/13fig18.jpg

Here's code for a selection list, auto-generated by Dreamweaver based on the dialog shown in Figure 13.18.

 <select name="productList" size="10" multiple>   <cfoutput query="qProducts">     <option value="#qProducts.ProductID#"      <cfif (qProducts.ProductID EQ 1)>selected</cfif>>      #qProducts.ProductName#</option>   </cfoutput> </select> 

This code uses the ColdFusion cfif tag to include the selected attribute only if the stated condition is met, namely that the dynamic data, qProducts.ProductID , is equal to 1. It also uses the ColdFusion cfoutput tag to display the items in the Recordset as the items on the selection list.

Live Data: Viewing Data While Editing

By default, Design View shows static content, but not dynamic data. However, because users will be seeing the dynamic data, you may want to see it while you're working, too. You can do that by clicking the Live Data button. Figure 13.19 shows qProducts_form.cfm (on the CD accompanying this book) with and without live data.

Figure 13.19. A dynamic text field, check box, radio group, and list/menu before and after selecting the Live Data option. After modifying dynamic content on the Server Behaviors panel, click the Refresh button to redisplay dynamic content. Alternatively, enable Auto Refresh by clicking the check box, and dynamic content will redisplay automatically.
graphics/13fig19.jpg

NOTE

Links don't work in Live Data mode. You have to view the page in a browser to test links.


Most normal Dreamweaver editing tasks can be performed in Live Data mode. For instance, you can edit or delete static text, or change the properties of a table or a list/menu in the Property inspector. In addition, you can modify dynamic content by editing server behaviors, as described in the next section.

Using Server Behaviors to Modify Dynamic Data

After you have created any of the dynamic content objects (dynamic table, text, text field, check box, radio group, list/menu), you can edit them in the Server Behaviors panel in the Application panel group. In the Server Behaviors panel, you can see a list of all the dynamic objects on the current page. Double-click on any one of them to bring up the same dialog that you used to create the object originally. Make any changes you want, click OK, and the object will be modified.

You can also delete objects in the Server Behaviors panel. Just select the object and press the Delete key. If other objects in the document depend on the object you are about to delete (for instance, you're about to delete a Recordset that is used to populate a dynamic table), Dreamweaver warns you, and you have to confirm the deletion.

When you make changes to the dynamic data, Dreamweaver normally stops displaying live data. To display live data again, click the Refresh button in the upper-left corner of the screen. (Refer to Figure 13.19.) To save yourself the trouble of clicking the Refresh button, enable auto-refresh by checking the Auto-refresh check box to the right of the Refresh button. (Again, refer to Figure 13.19.) A potential downside of auto-refresh is a delay while dynamic data is refreshed.

Inserting, Updating, and Deleting Database Records

Dreamweaver makes it relatively easy to create pages to insert, update, and delete database records. For all three operations, you start by creating a new page (File, New). Choose the Dynamic Page category in the first column, choose ColdFusion in the second column, and click the Create button.

With just a few mouse clicks, you can create a record insertion page that contains a form allowing the user to fill in information to populate a record. It also contains ColdFusion code for inserting the new record into the database.

In the Application category of the Insert bar, select Record Insertion Form Wizard by using the Insert Record button. (It's the fourth button from the right ”see Figure 13.20.)

Figure 13.20. The Application category on the Insert bar provides access to many commonly used dynamic data features. Buttons with downward-facing triangles are multi-purpose buttons. Click the triangle to expand the list of options associated with the button. Selections persist until you change them.
graphics/13fig20.jpg

The Record Insertion Form dialog appears, as shown in Figure 13.21. The only fields that are always required are the data source and a table. If the database is password protected, you'll also need a username and password. All the fields in the table are included in the insertion form by default. Select a field and click the minus sign to remove that field from the record insertion form. When the new record is inserted, that field either is blank or has the default content defined by the database. Click the plus sign, pick one or more field names on the Add Columns dialog, and click OK to re-include those fields in the insertion form. You can also define a page where the user is redirected after inserting the record.

Figure 13.21. Using the information you provide on the Record Insertion Form, Dreamweaver auto-generates code for inserting a record in the database.

graphics/13fig21.jpg


When you click OK on the Record Insertion Form dialog, Dreamweaver creates the form.

A record update page presents the user with a form showing the data currently contained in a record. The user can use the same form to change the data in one or more fields. When the user is satisfied with all changes, he or she clicks an Update record button to update the record.

Before you can create a page to update a record, you first have to create a Recordset containing the record you want to update. Then choose Record Update Form Wizard by using the Update Record button on the Insert bar. (The third button from the right in Figure 13.20.)

The Record Update Form dialog appears. The information required is similar to the Record Insertion Form, except that you also have to specify a Unique key column, a column (field) containing data that uniquely identifies each record in the table. This would typically be a primary key with no duplicates, such as ProductID in the Products table in the greendept database. The dialog provides a drop-down list of columns that could be used. Often there is only one such column, in which case it is automatically selected. This unique column is used in the record update form to unambiguously identify the record to update.

To create a page to delete a record, click on the Delete Record button and provide the name of the database (for example, greendept) and the name of the table (for example, Products). Optionally, you can also specify a page to go to after the record is deleted. (See Figure 13.22.)

Figure 13.22. The Delete Record dialog creates a page for deleting a record from a database.

graphics/13fig22.jpg


The Delete Record button doesn't create a form. Instead, it creates a block of ColdFusion code to delete a record. The record to be deleted must be identified by a primary key column, which is passed as a URL parameter to the page that performs the deletion. For example, the code for deleting a record from the Products table in the greendept database looks like this:

 <cfif IsDefined("URL.ProductID") AND #URL.ProductID# NEQ "">   <cfquery datasource="greendept">   DELETE FROM Products WHERE ProductID=#URL.ProductID#   </cfquery> </cfif> 

This code says, "If there is a URL parameter named ProductID, and it's not blank, then delete the record in the Products table in which the ProductID field is equal to the value passed in the URL parameter."

Drilling Down into Data with Master/Detail Pages

A common way to give users convenient access to data is to first show a master page providing brief identifying information for a number of records, along with links that allow the user to drill down to a detail page for any selected record. For example, the master page might have just the title and the author for a number of books. For any selected book, the user could drill down to get more detailed information, such as the publisher, the publication date, and so on. Dreamweaver can do much of the heavy lifting for creating such pages, auto-generating a master page (with multi-screen navigation, if necessary) and a detail page ”all based on one relatively simple dialog screen, shown in Figure 13.23.

Figure 13.23. The Master Detail Page Set dialog. Dreamweaver uses this information to auto-generate both a master and a detail page. If there are more records than can fit on one master page screen, Dreamweaver auto-generates navigation controls among master page screens.
graphics/13fig23.jpg

Both the master and detail pages are dynamic tables and can be edited in Code View or Design View.

Editing dynamic tables is discussed earlier in this chapter under "Creating and Editing a Dynamic Table," page 298 .


Before you can create master/detail pages, you first have to create a Recordset. Then choose Master Detail Page Set on the Insert bar. (The fifth button from the right in Figure 13.20.) Figure 13.23 explains how to fill in the Master Detail Page Set dialog.

You can browse to an existing page, and use that as the detail page. If you type in the name of a page that does not exist (for example, see detail in Figure 13.23), Dreamweaver creates it and opens it.

ColdFusion needs a unique key to pass from the master page to the detail page, to uniquely identify the desired record. For instance, ProductID is the unique key in Figure 13.23. ColdFusion passes the data in the unique key to the detail page as a URL parameter, such as recordID=240 here:

 http://localhost:8500/greendept/Detail.cfm?recordID=240 



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

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