Skip to main content
Participating Frequently
October 4, 2010
Question

Help with SQL insert

  • October 4, 2010
  • 2 replies
  • 1029 views

Hi,

Brand new to CF - using CF9. Have SQL Server 2005 database. Only one table. Using CF for frontend. Do not know proper syntax to get INSERT to work. Keep getting "Element XXXXXX is undefined in FORM."

Database name is RAWH.

Columns for insert are:

  • startdate
  • enddate
  • TypeHrs
  • NoOfHours
  • projects
  • justification

Frontend has forms and fields which are:

Form                                       Field (value)

firstDates                                startdate

firstDates                                enddate

typHrs                                     TypeHrs

noHrs                                      NoOfHours

none                                        projects

none                                        justification

Do not know how to code for INSERT - have this for now - which obviously does not work:

<cfquery name="RAWH" datasource="RAWH">
INSERT INTO RAWH
(startdate, enddate, TypeHrs, NoOfHours, projects, justification)
VALUES
(#Form.firstDates#, '#Form.startdate#', '#Form.enddate#', #Form.typHrs#, '#Form.TypeHrs#', #Form.noHrs#, '#Form.NoOfHours#')
</cfquery>

What am I doing wrong?

Get this:

Element FIRSTDATES is undefined in FORM.

The error occurred in D:\ColdFusion9\wwwroot\RAWH1\RAWH1.cfm: line 5
3 : (startdate, enddate, TypeHrs, NoOfHours, projects, justification)
4 : VALUES
5 : (#Form.firstDates#, '#Form.startdate#', '#Form.enddate#', #Form.typHrs#, '#Form.TypeHrs#', #Form.noHrs#, '#Form.NoOfHours#')
6 : </cfquery>
7 : 


Really appreciate any help - thank you. BTW - datasource is in CF Admin.

John

RAWH  Microsoft SQL Server  OK 

This topic has been closed for replies.

2 replies

Inspiring
October 4, 2010

The two most common reasons that error comes up are:

1.  You have a form, but it does not contain that field.

2.  You don't have a form.

To elaborate on number 2, you don't have a form until you submit one.   I suspect that's what's happening to you.

You are also doing some other things wrong including, but not limited to trying to put 7 values into 6 fields.

Participating Frequently
October 4, 2010

Ian & Dan,

Thanks. I added "<cfdump var="#form#">" and the result is "struct [empty]."

I altered the form/input code thusly,

<form id="beginDate" name="beginDate" onclick="function compareDate(); method="post"
               style="width: 1px; height: 1px;">
          <td><input style="width: 70px" type="text" id="startdate" name="startdate" /></td>
        </form>
        <form id="endnDate" name="endDate" onclick="function compareDate(); method="post"
               style="width: 1px; height: 1px;">
          <td><input style="width: 70px" type="text" id="enddate" name="enddate" /></td>
        </form>

Should this be a cfform? I also made separate forms for startdate and enddate rather than one form for both.

The cfquery is this:

<cfquery name="RAWH" datasource="RAWH">
INSERT INTO RAWH
(startdate, enddate, TypeHrs, NoOfHours, projects, justification)
VALUES
(#Form.beginDate#, '#Form.startdate#', '#Form.endDate#', '#Form.enddate#', #Form.typHrs#, '#Form.TypeHrs#', #Form.noHrs#, '#Form.NoOfHours#', 'projects', 'justification')
</cfquery>

Still the same result -

Element BEGINDATE is undefined in FORM.

The error occurred in D:\ColdFusion9\wwwroot\RAWH1\RAWH1.cfm: line 7
5 : (startdate, enddate, TypeHrs, NoOfHours, projects, justification)
6 : VALUES
7 : (#Form.beginDate#, '#Form.startdate#', '#Form.endDate#', '#Form.enddate#', #Form.typHrs#, '#Form.TypeHrs#', #Form.noHrs#, '#Form.NoOfHours#', 'projects', 'justification')
8 : </cfquery>
9 : 

What am I doing wrong?

Dan - you said that I do not have a form until I submit one - I get this error by opening the web page (F12). There is no onsubmit coding done.

Sorry guys, I am lost with this.

Thank you,

John

ilssac
Inspiring
October 4, 2010

With two seperate forms, you can only ever submit one or the other, never both at the same time (at least not without some more advanced AJAX techniques).

What Dan was telling you is that for the data to get from the client browser to the server to be acted on by ColdFusion, the form must be "Submitted".  Usually with a Submit button to be clicked on by the user, but can also be done with JavaScript code to fire the onSubmit() method on some action or the other in the form object of the browser's DOM (Document Object Model).

We are describing basic HTML and HTTP concepts that are not specific to ColdFusion.  You need a solid, basic handle on static HTML before you can do much with dynamic html created by ColdFusion CFML code.

ilssac
Inspiring
October 4, 2010

Test to make sure you are getting the form values you expect.  One really, really easy way to do this is to dump the form scope at the top of the INSERT action code.

<cfdump var="#form#">

Ensure that your form tag has a method="post" parameter.  If it does not, or if it has a method="get" in it, then the form data is going to be in the URL scope not the FORM scope.

HTH

Ian