Solving the mystery of missing records with a left join

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 8 docx (Trang 84 - 87)

The reason for the failure of quote_list.phpto display quotations without an associated author lies in the WHEREexpression:

WHERE quotations.author_id = authors.author_id

This works fine when there are matching records in both tables, but if the author_idfor- eign key hasn’t been set in the quotations table, there’s nothing to match it in the authors table. You need a way to find all records, even if there isn’t a corresponding match for the foreign key. This is achieved in SQL by what is known as aleft join.

The SQL queries generated by Dreamweaver are known as inner joins—there must be a complete match in both tables of all conditions in a WHEREexpression. The difference with a left join is that when there’s no match for a record in the table(s) to the “left” of the join, the result is still included in the recordset, but all the columns in the table to the “right” of the join are set to NULL. “Left” and “right” are used in the sense of which side of the key- words LEFT JOINthey appear in the SQL query. The syntax looks like this:

SELECT column_name(s) FROM first_table LEFT JOIN second_table ON condition

If the condition is matching two columns of the same name (such as author_id), an alter- native syntax can be used:

SELECT column_name(s) FROM first_table LEFT JOIN second_table USING (column_name)

Passing the primary key through a query string is something a lot of people seem to get wrong. Refer to “Updating and deleting records” in Chapter 14 if you need a reminder of how to do it.

You can now amend the SQL query in quote_list.phpto use a left join. Dreamweaver doesn’t have an automatic way of generating a left join, so you need to adjust the query manually. Continue working with quote_list.phpfrom the preceding section.

1.Highlight Recordset (quoteList)in the Server Behaviorspanel, and double-click it to open the Recordsetdialog box.

2.Edit the SQL query by hand like this:

SELECT quotations.quote_id, quotations.quotation, authors.first_name, authors.family_name

FROM quotations LEFT JOIN authors USING (author_id)

ORDER BY authors.family_name, authors.first_name, quotations.quotation 3.Click the Testbutton to make sure you haven’t made any mistakes in the query. I

find that I frequently forget to remove the comma after the first table name when replacing an inner join with a left join.

4.Click OKto save the recordset. Save the page, and refresh your browser. Any quo- tations without an author_idwill now appear at the top of the list with the Name column blank, as shown in Figure 16-17.

Figure 16-17.Using a left join finds records that don’t have a match in both tables.

Compare your code, if necessary, with quote_list_02.phpin examples/ch16.

Rather than build the update form from scratch, you can easily adapt the insert page again. Because you don’t need to check for duplicate entries, this is simpler than the update page for authors. After removing the Insert Record server behavior, you create a recordset for the record being updated, bind the existing values to the quotation text area and authordrop-down menu, and apply an Update Record server behavior.

1.Save quote_insert.php as quote_update.php. Change the title and heading to Update quotation. Also change the Button nameand Valueof the submit button to updateand Update quotation, respectively.

2.Select the Insert Recordserver behavior in the Server Behaviorspanel, and click the minus button to remove it.

Adapting the insert page for updates Using a left join to find incomplete records

16

3.When the EDITlink in quote_list.phpis clicked, you need to display the details of the record. Open the Recordsetdialog box in Simple mode, and create a recordset called getQuoteusing the following settings:

4.Expand Recordset (getQuote)in the Bindingspanel. Select the quotationtext area in the form, and then select quotationin the recordset. Click Bind.

5.You also need the author_iddrop-down menu to display the correct value. Select the menu object in the form, and click the Dynamicbutton in the Property inspec- tor. All the existing values are fine, but to display the selected value dynamically, click the lightning bolt icon to the right of the Select value equal tofield at the bot- tom of the dialog box.

In the Dynamic Data dialog box, select author_id from Recordset (getQuote), as shown in the following screenshot. Make sure you choose the correct recordset—

both of them include author_id. The other recordset contains all author_id numbers; you want only the specific one associated with the quotation identified by the URL query string.

Click OKtwice to close both dialog boxes. What you have just done creates the code to dynamically insert selected="selected"in the appropriate <option>tag to display the correct name from the authorstable.

6.Before adding the Update Record server behavior, you need to create a hidden form field to store the correct quote_id. Click in a blank area of the form, and insert a hidden field. In the Property inspector, name the hidden field quote_id, and click the lightning bolt icon to insert dynamic data in the Value field. Choose quote_idfrom Recordset (getQuote), and click OK.

7.Click the plus button on the Server Behaviorspanel, and choose Update Record. Use the following settings:

Submit values from:updateQuote Connection:connAdmin

Update table:quotations

After updating, go to:quote_list.php

8.Save the page, and test it. Compare your code, if necessary, with quote_update.php in examples/ch16.

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 8 docx (Trang 84 - 87)

Tải bản đầy đủ (PDF)

(94 trang)