|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
For this example, suppose that you are a developer for a credit card company that wants to Web-enable many of its customer service functions. The function you are tasked with coding is the change of address. As part of this function, you have to code the following form to collect the new address information: <FORM ACTION=updateaddr.cfm METHOD=POST> Please enter your credit card number: <INPUT TYPE=TEXT NAME=card_number> <P> Please enter your new address below: <P> Street: <INPUT TYPE=TEXT NAME=new_street SIZE=20> <P> City: <INPUT TYPE=TEXT NAME=new_city SIZE=20> <P> State: <INPUT TYPE=TEXT NAME=new_state SIZE=20> <P> Zip: <INPUT TYPE=TEXT NAME=new_zip SIZE=20> <P> <INPUT TYPE=SUBMIT VALUE=Update my Address> </FORM> In addition to the new address information, you also have to ask for something to uniquely identify the customer. In this case, you can use the credit card number itself because you know that each customer has a unique number. Now you need to write the template updateaddr.cfm to do the update operation and display a confirmation message so that customers know that their information was changed. To do the update, you would again use the <CFQUERY> tag: <CFQUERY DATASOURCE=customers NAME=UpdateAddr> UPDATE Contact_Info SET street=#Form.new_street#,city=#Form.new_city#, state=#Form.new_state#,zip=#Form.new_zip# WHERE customer_ID = #Form.card_number# </CFQUERY> The SQL UPDATE command is used to do the update, and you can see how each field is being set equal to the new values submitted on the form. Whats very important in the SQL statement is the WHERE clause, which restricts the update to the record belonging to the user with the credit card number entered on the form. Without the WHERE clause, the street, city, state, and zip columns of every record in the database would have been updated to reflect the new address information! The moral of the story is this: When doing updates, make sure that you are keying in on the record or records that require the updates by using the WHERE clause in your SQL statement. Otherwise, your updates will be made globallyusually with disastrous results.
Listing 34.3 shows a complete listing of one possible way to code the template updateaddr.cfm so that it does the update and notifies the user about the change. Listing 34.4 Automatic Deletion of Old Comments <CFQUERY DATASOURCE=feedback NAME=GetDeleteCandidates> SELECT visitor,comment FROM Comments WHERE entry_date < #CreateODBCDate(Now() - CreateTimeSpan(30,0,0,0))# </CFQUERY> <HTML> <HEAD> <TITLE>Deleted Comments</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <B>The following comments have been deleted:</B> <P> <TABLE> <TR><TH>Visitor Name</TH><TH>Comment</TH></TR> <CFOUTPUT QUERY=GetDeleteCandidates> <TR><TD>#visitor#</TD><TD>#comment#</TD></TR> </CFOUTPUT> </TABLE> </BODY> </HTML> <CFQUERY DATASOURCE=feedback NAME=PurgeOldComments> DELETE FROM Comments WHERE entry_date < #CreateODBCDate(Now() - CreateTimeSpan(30,0,0,0))# </CFQUERY>
Using Decision StatementsConditional logic is another basic component of any programming language, and ColdFusion enables you to build conditional processing into your applications with the <CFIF> tag. Instead of taking a particular attribute, the <CFIF> tag has to contain one of ColdFusions eight decision operators:
Each of these operators is evaluated to a Boolean TRUE or FALSE value. If the operator evaluates to TRUE, the code between the <CFIF> and </CFIF> tags is executed. Otherwise, the code is ignored.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |