...

View Full Version : Variable Undefined



macwiz
07-08-2009, 03:35 AM
Whenever I run this code, I get VARIABLE LNAME IS UNDEFINED


<cfquery name="TeacherFN" datasource="AITE_Test_System">
SELECT Lname, Fname
FROM dbo.[Teachers]
WHERE AD_Username = '#FORM.TeacherAD_Username#'
</cfquery>

<cfquery name="ClassEdit" datasource="AITE_Test_System">
<cfif #FORM.EditMode# IS "AddClass">
INSERT INTO dbo.[Classes] (Class_Name, Teacher_lname, Teacher_fname, Teacher_AD_Username, Period) VALUES ('#FORM.ClassName#', '#Lname#', '#Fname#', '#FORM.TeacherAD_Username#', '#FORM.Period#')
<cfelseif #FORM.EditMode# IS "EditClass">
UPDATE
<cfelseif #FORM.EditMode# IS "DeleteClass">
DELETE FROM dbo.[Classes] WHERE Fname='#Fname#' AND Lname='#Lname#' AND Teacher_AD_Username='#FORM.TeacherAD_Username#'
</cfif>
</cfquery>


Any idea why? THANKS!

Gjslick
07-08-2009, 03:56 PM
Yes I have a very good idea as to why it's saying that LNAME is undefined: because it's not :p

It seems that you are running a query named 'TeacherFN', and now you're trying to access the data from that query. But, the data columns retrieved by that query (Lname and Fname) are not just going to magically appear as variables in your cfm page. So, data can be accessed from a query in one of two ways:


1) You can use the columns of a query in a cfoutput query or cfloop query. By doing it this way, the data columns from the query will be available via the "query" scope:


<!--- In both these examples, the 'TeacherFN' query gets loaded into
the query scope, so now the Lname and Fname data columns are
available as variables --->

<cfoutput query="TeacherFN">
#Lname# #Fname#
</cfoutput>

<!--- Same thing as above --->
<cfloop query="TeacherFN">
#Lname# #Fname#
</cfloop>
However, the catch to this is that both of these tags will "loop" over the query. That is in your case, if 'TeacherFN' returns more than one record, your code is going to be generating more than one INSERT/UPDATE/DELETE statement; one for each row returned by 'TeacherFN'. But, it looks like the 'TeacherFN' query is only supposed to return one row, so I recommend using the next method.


2) You can access the data returned by a query directly by retrieving them in the form: #queryName.columnName#. So, 'Lname' and 'Fname' from this query are available in your cfm page as #TeacherFN.Lname# and #TeacherFN.Fname#.

Your resulting code (with a bit of formatting) will look like this:


<cfquery name="TeacherFN" datasource="AITE_Test_System">
SELECT Lname, Fname
FROM dbo.[Teachers]
WHERE AD_Username = '#FORM.TeacherAD_Username#'
</cfquery>

<cfquery name="ClassEdit" datasource="AITE_Test_System">
<cfif #FORM.EditMode# IS "AddClass">
INSERT INTO dbo.[Classes] (
Class_Name,
Teacher_lname,
Teacher_fname,
Teacher_AD_Username,
Period
) VALUES (
'#FORM.ClassName#',
'#TeacherFN.Lname#',
'#TeacherFN.Fname#',
'#FORM.TeacherAD_Username#',
'#FORM.Period#'
)
<cfelseif #FORM.EditMode# IS "EditClass">
UPDATE
<!--- Apparently your update statement is incomplete... --->
<cfelseif #FORM.EditMode# IS "DeleteClass">
DELETE FROM
dbo.[Classes]
WHERE
Fname = '#TeacherFN.Fname#'
AND Lname = '#TeacherFN.Lname#'
AND Teacher_AD_Username = '#FORM.TeacherAD_Username#'
</cfif>
</cfquery>
One last note: It is always good to retrieve the data returned by queries by putting it in the queryName.columnName form, even when getting the data via a cfoutput query or cfloop query. This helps tremendously in code readability, because you know that a variable is coming from query data and is not just a variable defined elsewhere. Ex:


<cfoutput query="TeacherFN">
#TeacherFN.Lname# #TeacherFN.Fname#
</cfoutput>
Hope that all helps!

Greg

macwiz
07-10-2009, 06:00 AM
I figured it out. thanks anyways.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum