Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > ColdFusion

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-08-2009, 03:35 AM   PM User | #1
macwiz
Regular Coder

 
Join Date: Jul 2008
Posts: 195
Thanks: 3
Thanked 11 Times in 11 Posts
macwiz is on a distinguished road
Variable Undefined

Whenever I run this code, I get VARIABLE LNAME IS UNDEFINED
PHP Code:
<cfquery name="TeacherFN" datasource="AITE_Test_System">
    
SELECT LnameFname
    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_NameTeacher_lnameTeacher_fnameTeacher_AD_UsernamePeriodVALUES ('#FORM.ClassName#''#Lname#''#Fname#''#FORM.TeacherAD_Username#''#FORM.Period#')
<
cfelseif #FORM.EditMode# IS "EditClass">
    
UPDATE 
<cfelseif #FORM.EditMode# IS "DeleteClass">
    
DELETE FROM dbo.[ClassesWHERE Fname='#Fname#' AND Lname='#Lname#' AND Teacher_AD_Username='#FORM.TeacherAD_Username#'
</cfif>
</
cfquery
Any idea why? THANKS!
__________________
-MacWiz1220
Science fiction does not remain fiction for long. And certainly not on the Internet.
-Vinton Cerf: Chief Internet Evangelist for Google and co-creator of the TCP/IP Internet Protocol Suite
macwiz is offline   Reply With Quote
Old 07-08-2009, 03:56 PM   PM User | #2
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
Yes I have a very good idea as to why it's saying that LNAME is undefined: because it's not

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:
Code:
<!--- 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:
Code:
<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:
Code:
<cfoutput query="TeacherFN">
  #TeacherFN.Lname# #TeacherFN.Fname#  
</cfoutput>
Hope that all helps!

Greg
Gjslick is offline   Reply With Quote
Old 07-10-2009, 06:00 AM   PM User | #3
macwiz
Regular Coder

 
Join Date: Jul 2008
Posts: 195
Thanks: 3
Thanked 11 Times in 11 Posts
macwiz is on a distinguished road
I figured it out. thanks anyways.
__________________
-MacWiz1220
Science fiction does not remain fiction for long. And certainly not on the Internet.
-Vinton Cerf: Chief Internet Evangelist for Google and co-creator of the TCP/IP Internet Protocol Suite
macwiz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:21 PM.


Advertisement
Log in to turn off these ads.