Reputation: 739
I have an interesting problem here...
<cfloop from="1" to="#form.countField#" index="i">
<cfif isdefined('form["semester#i#"]')>
<cfquery name = "insertCourses" datasource="cas_evaluation">
INSERT INTO courses (faculty, semester, course, students, hours, team_taught, first_time, ec_dl, online, course_revision )
VALUES ( '#form.name#', '#form['semester#i#']#', '#form['course#i#']#', '#form['numstudents#i#']#', '#form['hours#i#']#', '#form['team#i#']#', '#form['firsttime#i#']#', '#form['ec_dl#i#']#', '#form['online#i#']#', '#form['revision#i#']#')
</cfquery>
</cfif>
</cfloop>
Basically, I have some dynamic fields that can be added in or deleted. (These are rows of fields btw...) The way I have it coded...if the user deletes a row in the middle... ( they delete row 2 and rows 1 and 3 are left...) it causes problems because the loop is looking for it but it is not there obviously. So I tried checking to see if one of the fields were defined...but it doesn't like the syntax of the isdefined variable.. :(
any suggestions?
Upvotes: 3
Views: 6644
Reputation: 4006
Also, make sure you use cfqueryparam for escaping your sql variables. The code you have at the moment is full of sql injection holes. This code should work and should be safe.
<cfloop from="1" to="#form.countField#" index="i">
<cfif structKeyExists(form, 'semester#i#')>
<cfquery name = "insertCourses" datasource="cas_evaluation">
INSERT INTO courses
(faculty, semester, course, students, hours, team_taught, first_time, ec_dl, online, course_revision)
VALUES
( <cfqueryparam cfsqltype='cf_sql_varchar' value='#form.name#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['semester#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['course#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['numstudents#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['hours#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['team#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['firsttime#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['ec_dl#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['online#i#']#' />
, <cfqueryparam cfsqltype='cf_sql_varchar' value='#form['revision#i#']#' />
);
</cfquery>
</cfif>
</cfloop>
Upvotes: 1
Reputation: 4006
I don't quite understand the question. So this isn't working?
<cfif isdefined('form["semester#i#"]')>
Use
<cfif structKeyExists(form, "semester#i#")>
cfparam and isDefined don't like the array style syntax. In your case you could also use:
<cfif isdefined('form.semester#i#')>
Personally it's not a style I like myself but it should work ok.
Upvotes: 9
Reputation: 10857
Use the Form scope like a structure. If you use structKeyList(form), you will get a list of every form field defined.
Upvotes: 2