Dynamic SQL
99
<td>#FirstName#</td>
<td>#LastName#</td>
<td>#DollarFormat(Salary)#</td>
<td>#Contract#</td>
</tr>
</cfoutput>
</table>
</body>
</html>
3
Save the page as
getemp.cfm
.
4
Open the file
askemp.cfm
in your browser and enter criteria into any fields, then
submit the form.
5
Verify that the results meet the criteria you specify.
Reviewing the code
The action page
getemp.cfm
builds a SQL statement dynamically based on what the
user enters in the form page AskEmp.cfm. The following table describes the
highlighted code and its function:
CFML Code
Description
SELECT *
FROM Employee
WHERE
Get the records from the Employee table
according to the following conditions.
<cfif #Form.FirstName# is not "">
Employee.FirstName LIKE
’#form.FirstName#%’ AND
</cfif>
If the user entered anything in the
FirstName text box in the form, add "AND
Employee.FirstName LIKE ‘[what the user
entered in the FirstName text box]%'" to the
SQL statement. You can use the FirstName
variable without ensuring its existence
because text boxes pass an empty string if
you do not enter text.
<cfif #Form.LastName# is not "">
Employee.LastName LIKE
’#form.LastName#%’ AND
</cfif>
If the user entered anything in the
LastName text box in the form, add "AND
Employee.LastName LIKE ‘[what the user
entered in the LastName text box]%'" to the
SQL statement.
<cfif #Form.Salary# is not "">
Employee.Salary >=
#form.Salary# AND
</cfif>
If the user entered anything in the Salary
text box in the form, add "AND
Employee.Salary >= [what the user entered
in the Salary text box]" to the SQL
statement.
Summary of Contents for COLDFUSION 5-DEVELOPING
Page 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Page 58: ...38 Chapter 3 Querying a Database ...
Page 134: ...114 Chapter 7 Updating Your Database ...
Page 210: ...190 Chapter 10 Reusing Code ...
Page 232: ...212 Chapter 11 Preventing and Handling Errors ...
Page 238: ...218 Chapter 12 Using the Application Framework ...
Page 262: ...242 Chapter 12 Using the Application Framework ...
Page 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Page 320: ...300 Chapter 15 Indexing and Searching Data ...
Page 336: ...316 Chapter 16 Sending and Receiving E mail ...
Page 374: ...354 Chapter 18 Interacting with Remote Servers ...