Inserting a Record


If you or other users need to be able to easily add records to a database table, you'll want to create a Web page from which to add them. The process of creating Dreamweaver pages that insert new records in a database table is the same for all server technologies that are supported by Dreamweaver.

You can use one of two methods to insert records using a Web page: You can create a form and add the Insert Record server behavior, or you can use the Record Insertion Form Wizard application object to automatically add a form and the server behavior.

In the first task in this section, you'll add a new member to the members table in the travelclub database by creating a PHP page, inserting an HTML form, and adding an Insert Record server behavior. If you want to customize the HTML form used to insert the record, you'll have more options if you use this method. As you may recall from Chapter 5, the members table includes fields with basic information about the travel club members, such as id, first_name, last_name, and email (Figure 12.1). The id field is the primary key in the members table.

Figure 12.1. The MySQL Monitor shows the current data in the members table of the travelclub database.


In the second task, you'll create a confirmation page that displays when the user submits the form information.

In the final task, you'll add a new member to the members table in the travelclub database by creating a PHP page and using a Record Insertion Form Wizard application object to add a form and an Insert Record server behavior in one step. This is the fastest and easiest way to insert a new record using a Web page.

To insert a record using a server behavior:

1.

Open a new PHP page in Dreamweaver by choosing File > New > Dynamic page > PHP. Save the page as record1.php.

Now we'll insert a form and add four text fields so that a user can enter his or her ID, first name, last name, and e-mail address.

2.

From the Insert menu, select Form > Form to add a form to your page (Figure 12.2).

Figure 12.2. Choose Insert > Form > Form to insert a new form on your page.


3.

In Design view, select the form. In the Property inspector, type a name for the form in the Form Name box (Figure 12.3). Since this form is used to enroll new members in the travel club, we named the form newMembers.

Figure 12.3. In the Property inspector, type a name for the form in the Form Name box.


You don't need to specify an action or a method for the formthese will be set automatically when you add the Insert Record server behavior.

4.

From the Insert menu, select Form > Text Field to add a text field to the form.

5.

In Design view, select the text field.

6.

In the Property inspector, type a name for the field in the TextField name box. Since the first text field is used to collect the new member's travelclub ID, we named the textfield id.

7.

In the Char Width and the Max Chars fields, type 5 to limit the size of the field to five characters (Figure 12.4).

Figure 12.4. Type id in the TextField name box, and enter 5 in the Char Width and Max Chars fields. For the Type option, use the default selection (Single Line).


The id field in the members table has a size of 5, so we used this same size for the text field (Figure 12.5).

Figure 12.5. The MySQL Monitor displays a MySQL description of the fields in the members table.


8.

In the Type section, select the Single Line radio button.

9.

Repeat Steps 4 through 8 to add text fields for first name, last name, and e-mail address.

Each of these fields should have a character width of 30, the same as the size of those fields in the database tables.

10.

From the Insert menu, select Form > Button to insert a Submit button.

The newMembers form is now complete. Next, you'll add the Insert Record server behavior.

11.

In the Application panel group, click the Server Behaviors tab to access the Server Behaviors panel.

12.

Click the plus button and select Insert Record from the menu (Figure 12.6).

Figure 12.6. On the Servers Behaviors panel, click the plus button and select Insert Record from the menu.


The Insert Record dialog appears.

13.

From the Submit Values From dropdown list, select the name of the form, newMembers (Figure 12.7).

Figure 12.7. Select newMembers from the drop-down list.


14.

From the Connection drop-down list, select the name of your connection to the travelclub database. Our connection is named connTravelClub.

15.

From the Insert Table drop-down list, select the members table.

The Columns section is automatically filled in for you, and items in the Value and Submit As pop-up lists are selected, based on your entries in the Property inspector for each of the text fields.

16.

In the After Inserting, Go To field, type a name for the page to display after the user has submitted the form. We used the name confirmation_newMember.php. You'll create this page in the next task.

17.

Click OK to insert the behavior and close the dialog.

You've created an HTML form to gather information from new members of the travel club. The information the user submits in the form is used to create a new record in the members table of the travelclub database.

In the next task, you'll create a confirmation page and test the form by previewing the page in a browser.

Tips

  • If you don't see the form boundaries in Design view, you need to enable Invisible Elements. From the main Dreamweaver menu, choose View > Visual Aids > Invisible Elements.

  • If you want to make sure that the user fills out all the form fields and the e-mail address format is valid, add JavaScript validation behaviors to the form fields. See "Validating Forms," in Chapter 8.


To create a confirmation page:

1.

Open a new PHP page in Dreamweaver by choosing File > New > Dynamic page > PHP. Save the page as confirmation_ newMember.php or whatever name you chose for the confirmation page in Step 16 of the preceding task.

2.

Enter a message on the page, such as "Thank you for enrolling in the travel club. We will contact you within 72 hours to confirm your membership." Save the page.

3.

Open the record1.php page you created in the preceding task. From the main Dreamweaver menu, choose File > Preview in Browser. Enter values for all the fields and submit the form (Figure 12.8).

Figure 12.8. Preview the form in a browser. Type values in each of the form fields and click the submit button.


The confirmation page displays (Figure 12.9).

Figure 12.9. When the submit button is clicked, the confirmation page appears.


Tips

  • If the confirmation page doesn't display after you submit the form, open record1.php in Dreamweaver. On the Server Behaviors panel, double-click Insert Record. The Insert Record dialog appears. Make sure the information in this dialog is correct, particularly the name of the confirmation page.

  • To confirm that the new member's record has been added to the database table, open the MySQL Monitor, and at the mysql prompt, type use travelclub; Then at the next prompt, type SELECT * FROM members; All the current data in the members table appears.


To insert a record using an application object:

1.

Open a new PHP page in Dreamweaver by choosing File > New > Dynamic page > PHP. Save the page as record2.php.

2.

From the Insert menu, select Application Objects > Insert Record > Record Insertion Form Wizard (Figure 12.10).

Figure 12.10. Choose Insert > Application Objects > Insert Record > Record Insertion Form Wizard to insert a form and add an Insert Record server behavior in one step.


The Record Insertion Form dialog appears. Now we'll fill in the dialog to insert a form and an Insert Record server behavior.

3.

From the Connection drop-down list, select the name of your connection to the travelclub database (Figure 12.11). Our connection is named connTravelClub.

Figure 12.11. In the Record Insertion Form dialog, choose the connection and the database table, and enter a name for the confirmation page.


4.

From the Table drop-down list, select the members table.

5.

In the After Inserting, Go To field, type a name for the page to display after the user has submitted the form. We used the name confirmation_newMember.php.

You can use the same confirmation page for all the tasks in this section.

6.

In the Form Fields section, all the fields in the members table are listed. Use the plus and minus buttons to add or delete a field, and use the up and down arrow buttons to move a field up or down in the list. To change the label, display format, or data format, select an item in the list, type a new value in the Label field, and make new selections from the Display As and Submit As drop-down lists.

The label specifies how the form object is labeled on the page. We changed the labels for all the fields. For example, we changed the id field label to ID. The Display As field specifies the type of form object used to collect information for that field. For example, we used text fields for all the items. The Submit As field specifies the type of data that's sent to the database, such as Numeric for the id field.

7.

Click OK to close the dialog and insert the form and the Insert Record server behavior.

The form appears in Design view (Figure 12.12).

Figure 12.12. The Record Insertion Form Wizard application object automatically inserts a form on your page.


8.

Save the page.

9.

Test the page in a browser. From the main Dreamweaver menu, choose File > Preview in Browser. Enter values in the form and click the Insert Record button. The confirmation page should appear (Figure 12.9).

You've inserted an application object that created an HTML form to gather information from new members of the travel club. The information the user enters and submits with the form creates a new record in the members table of the travelclub database.




Macromedia Dreamweaver 8 Advanced for Windows and Macintosh. Visual Quickpro Guide
Macromedia Dreamweaver 8 Advanced for Windows and Macintosh: Visual QuickPro Guide
ISBN: 0321384024
EAN: 2147483647
Year: 2004
Pages: 129
Authors: Lucinda Dykes

Similar book on Amazon

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