In: Categories » Computers and technology » Programming » Modifying a Company in the Database
The data that CFQUERY requests from the database comes back in a result set, and the Name attribute tells ColdFusion what that result set is to be named. The SQL statement consists of three clauses: SELECT, FROM, and ORDER BY. SELECT tells the database which columns to retrieve from the database; FROM tells the database which table to retrieve those columns from; and ORDER BY tells the database how to sort the results. The result set returned from CFQUERY contains multiple rows of data, and each row has multiple columns. It would appear that the CFOUTPUT in CompanyList.cfm evaluates variables as the CFOUTPUT in Finished.cfm did. The CFOUTPUT in CompanyList.cfm, however, has a Query attribute, which tells ColdFusion to execute everything between <cfoutput> and </cfoutput> once for each row in the result set, so you end up with one HTML table row for every row in the result set. Now that you can add and list companies, you need to the capability to modify them as well. The edit process is much like the add process, with some additional code. Before the user can edit a company record, he needs to retrieve it from the database and load it into a form. In this application, the user chooses a company to edit by entering its CompanyID into one form, submitting it, and editing it in a second form. Create a file named CompanyGetEditForm.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file.
<html> <head> <title>ColdFusion MX Bible</title> <link rel=”stylesheet” href=”styles.css”> </head> <body> <h1>Edit a Company</h1> <table> <cfform action=”CompanyEditForm.cfm” method=”POST”> <tr> <td>Company ID</td> <td> <cfinput type=”Text” name=”CompanyID” message=”Please enter the Company ID” validate=”integer” required=”Yes” size=”22” maxlength=”20”> </td> </tr> <tr> <td> </td> <td> <input type=”submit” value=”Get Company”> </td> </tr> </cfform> </table> </body> </html>
The CFFORM has a single CFINPUT for entering the ID of the company that you want to edit. This form submits its data to the next form that you’re going to build, which is where you’ll edit the company. The company edit form is much like the company add form, but the edit form must first retrieve the company record from the database and place that data into the form fields. Create a file named CompanyEditForm.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file.
<cfquery name=”GetCompany” datasource=”#Request.MainDSN#”> SELECT CompanyName, Address, City, State, ZipCode, Comments FROM Company WHERE CompanyID = #Val(CompanyID)# </cfquery> <html> <head> <title>ColdFusion MX Bible</title> <link rel=”stylesheet” href=”styles.css”> </head> <body> <h1>Edit a Company</h1> <table> <cfform action=”CompanyEditAction.cfm” method=”POST”> <cfoutput> <input type=”hidden” name=”CompanyID” value=”#Val(CompanyID)#”> </cfoutput> <tr> <td>Company Name</td> <td> <cfinput type=”Text” name=”CompanyName” value=”#GetCompany.CompanyName#” message=”Please enter a name for this company.” required=”Yes” size=”40” maxlength=”40”> </td> </tr> <tr> <td>Address</td> <td> <cfinput type=”Text” name=”Address” value=”#GetCompany.Address#” message=”Please enter this new Company’s Address.”
required=”Yes” size=”32” maxlength=”30”> </td> </tr> <tr> <td>City</td> <td> <cfinput type=”Text” name=”City” value=”#GetCompany.City#” message=”Please enter a city.” required=”Yes” size=”22” maxlength=”20”> </td> </tr> <tr> <td>State</td> <td> <cfinput type=”Text” name=”State” value=”#GetCompany.State#” message=”Please enter a state.” required=”Yes” size=”3” maxlength=”2”> </td> </tr> <tr> <td>ZIP Code</td> <td> <cfinput type=”Text” name=”ZipCode” value=”#GetCompany.ZipCode#” message=”Please enter a valid ZIP Code.” validate=”zipcode” required=”Yes” size=”11” maxlength=”10”> </td> </tr> <tr> <td>Comments</td> <td> <textarea cols=”40” rows=”5” name=”Comments”><cfoutput>#GetCompany.Comments#</cfoutput></textarea> </td> </tr> <tr> <td> </td> <td>
<input type=”submit” value=”Update Database”> </td> </tr> </cfform> </table> </body> </html>
The edit form retrieves the company record and populates the form fields with the current data.
legal notice
Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.
Useful tools and features
related articles
You learn by doing, and then we go back and explain how ColdFusion worked its magic. Before you get started, though, you must learn a few terms, set up the database, and create the Web directory that you’re going to use. These are some terms that you should know: A template is a file with a .cfm extension. ColdFusion executes these templates and produces HTML that is returned to the user’s browser. A page is what appears in your browser. It is rendered from the HTML that ColdFusion Server sends back ...
2. Building the company add action template
In this article, you create the template that puts your form data into the database. Create a file named CompanyAddAction.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfquery name=”InsertCompany” datasource=”#Request.MainDSN#”> INSERT INTO Company( CompanyName, Address, City, State, ZipCode, Comments ) VALUES ( ‘#Trim(Form.CompanyName)#’, ‘#Trim(Form.Address)#’, ...
3. Building the company edit action template
The code in CompanyEditAction.cfm is like the code in CompanyAddAction.cfm, but the edit action updates rather than inserts. Create a file named CompanyEditAction.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfquery name=”UpdateCompany” datasource=”#Request.MainDSN#”> UPDATE Company SET CompanyName = ‘#Trim(Form.CompanyName)#’, Address = ‘#Trim(Form.Address)#’, City = ‘#Trim(Form.City)...
4. Adding a New Employee to the Database
The CFQUERY in CompanyDeleteAction.cfm uses a DELETE statement with two SQL clauses: DELETE and WHERE. DELETE tells the database the table from which to delete a record, and WHERE tells the database which record to delete. You can watch the company delete process in action. Point your Web browser to http://<yourserver>/CFMXBible/Ch02/CompanyGetDeleteForm.cfm and enter a CompanyID. (To get a valid ID, go to the company list and pick a number from the ID column.) Click Submit to see the chosen company’s informatio...
5. Modifying an Employee in the Database
The DateFormat() function around the DateOfBirth column in the codereturns the employee’s birth date reformatted according to a display mask. DateOfBirth normally comes back from the database in the following format: 2002-01-01 00:00:00.0 That format is not very user-friendly. Calling DateFormat() with a mask of “mm/dd/yyyy” returns the date as follows: 01/01/2002 This version is, of course, more natural and easy to read. The same is true for the employee edit process...
6. Removing an Employee From the Database
The user must have the capability to remove employees from the database. The employee delete process is a simple combination of techniques that you have already learned, such as retrieving a record from the database, displaying that record in a template, and so on. The first page in this process is nearly identical to the employee get edit form. Create a file named EmployeeGetDeleteForm.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <html> <head> <ti...
7. Making direct links to the forms
Say that you want to modify or delete a company. Right now, you need to remember the company’s ID, go back to the launch pad, click Company Edit, and enter the company ID, all just to get to the edit form. Wouldn’t you rather click a company in the list and go directly to the edit form? ... <table> <tr> <td><b>ID</b></td> <td><b>Name</b></td> <td><b>Address</b></td> <td><b>City</b></t...
8. Using a Custom Tag
Custom tags are reusable, developer-authored extensions to the ColdFusion language. The custom tag that you create in the following parts displays today’s date in a familiar format. Create a file named TodaysDate.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfoutput>#DateFormat(Now(), “ddd, mmm d, yyyy”)#</cfoutput> Now() returns the current date and time, and DateFormat() reformats the date. To call your custom tag, open index.cfm, a...
9. Creating your physical data model
Physical data models are extracted from their logical counterparts by mapping logical objects to physical objects supported by the target database platform. The following table shows the relationship between logical and physical objects. In many cases, your physical data model almost mirrors your logical data model because entities typically map directly to the tables that store them. Exceptions to this rule are logical data models that contain nonspecific relationships and entity subtypes. Although entity subtypes are an advanced top...
10. Understanding All Relational Result Sets
This article can help you better understand complicated SQL containing multitable joins, group-related clauses, and aggregate functions—by far the most problematic topics for most database developers. You also learn the correct way to handle database exceptions and incorporate them as actual functionality in your ColdFusion application. You learn, too, how to increase performance by caching queries in memory for fast access. You can memorize SQL clauses and Bachus-Naur forms until you’re blue in the face, ...
