Inserting the Data into a Database
In this task, you'll insert the username and score into the database. To do so, you'll have to hand-code a SQL query.
1. | Still in dante_quiz_results.cfm, switch to code view and scroll to the top of the page. Add several blank lines above the opening <!DOCTYPE> tag. |
The query is not a part of th218 output, so it makes sense to put it outside th218 document. By putting the query above the beginning of th218 document, you ensure that if for some reason the query doesn't work, the rest of the page will not process either, but will instead display an error message.[View full size image]

2. | Begin the query by typing the following code: <cfquery name="insertQuizScores" datasource="dante"> |
Leave a blank line between these two lines.[View full size image]

INSERT INTO someTableThe first set of items lists the names of the fields in the database table into which you want to insert data. The second set of items lists the data that you want inserted. The two sets are correlated; in this example, 'Value1' will be inserted into fieldName1 of the someTable table; 'Value2' will be inserted into fieldName2, and so on.To insert the quiz data from the Flash quiz into the database, you'll need something like this:
(
fieldName1,
fieldName2,
fieldName3
)
VALUES
(
'Value1',
'Value2',
'Value3'
)
INSERT INTO quizThe tricky part is the part in the square brackets; you'll need to figure out how to get ColdFusion to supply that data for you.
(
username,
score
)
VALUES
(
'[the username supplied in the URL variable]',
'[the score supplied in the URL variable]'
)
3. | Between the opening and closing <cfquery> tags, insert the following code: INSERT INTO quiz |
Just as it does with the <cfoutput> blocks you've seen before, ColdFusion evaluates the variables inside the pound signs (##) before it sends the results to the database. Thus, when this command is sent to the database, the variables have already been replaced with their actual values.[View full size image]

4. | Save and upload dante_quiz_results.cfm. Test dante_quiz_login.cfm and take the quiz again. After you're finished, leave the browser open. |
It works! How do you know? If you don't see an error, it must have worked. If ColdFusion had any trouble, it would have generated an error message. But an even more reliable way to verify that your data has been successfully inserted is just to go in and look at the table.[View full size image]

In the View Data window, you'll see all the records in the quiz database. The email address and the score you just created are listed. Success!