Skip to main content
Inspiring
March 20, 2007
Question

simple cfinsert not working

  • March 20, 2007
  • 5 replies
  • 828 views
i'm really pulling my hair out now!

normally i build an html form and let dw create the cf code to insert the forms contents into my access db, ive done this loads of times, however today it does not want to work. ive scrapped the dw code and written my own to make it smaller and easier to read (this code still doesnt work). i have posted the cf code below

<cftransaction>
<cfquery name="insert_new_admin" datasource="allieddatabase">
INSERT INTO users1 (name, department, username, password, confirmpassword}
VALUES ('new_admin_user.name', 'new_admin_user.department', 'new_admin_userusername', 'new_admin_user.password', 'new_admin_user.confirmpassword')

</cfquery>
</cftransaction>


and here is the html code:

<form action="newadminaction.cfm" method="POST" name="new_admin_user">
<label for="name">Name</label>
<input type="text" name="name" id="name">
<label for="department">Department</label>
<select name="department" id="app_select">
<option selected>OPS</option>
<option>Sales</option>
<option>Accounts</option>
<option>P&W</option>
<option>General management</option>
</select>
<label for="username">user name</label>
<input type="text" name="username" id="username">
<label for="password">Password</label>
<input type="text" name="password" id="password">
<label for="confirmpassword">Confirm Password</label>
<input type="text" name="confirmpassword" id="confirmpassword">
<label for="level">Level</label>
<select name="level" id="app_select">
<option selected>1</option>
<option>2</option>
<option>3</option>
<option>999</option>
</select>
<br>
<input name="Submit" type="submit" id="submit_button" value="Submit">
<input type="hidden" name="MM_InsertRecord" value="new_admin_user">

</form>

all i get is this:

Syntax error in INSERT INTO statement.

with lots of other stuff underneith it


tried loads of different combinations, has anyone got any ideas?
This topic has been closed for replies.

5 replies

Inspiring
March 21, 2007
i think the problem is with NULL, not </cfif>...
which field are you trying to insert NULL into? check the properties for
that field in your db to make sure it allows NULL as a value and that it
does not require a value to be entered...
--

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
zac1234Author
Inspiring
March 21, 2007
actually i'm not sure why the NULL is there, can i just delete the NULLs? why has DW put the NULL there?
Inspiring
March 21, 2007
quote:

Originally posted by: zac1234
actually i'm not sure why the NULL is there, can i just delete the NULLs? why has DW put the NULL there?

Generally speaking, the null is there to account for empty form fields. If you have an empty form field and the db field is char, you insert an empty string which may be ok or it may not be ok. However, if the field is numeric or date, the insert crashes.
zac1234Author
Inspiring
March 21, 2007
ok done this, changed the password field to another name, gone back to DW and let it create my code. i now get the following.....


Error Executing Database Query.
Syntax error in INSERT INTO statement.

The error occurred in F:\data\webdesigns\wwwroot\CFIDE\Allied\newadminuser.cfm: line 40

38 : <cfelse>
39 : NULL
40 : </cfif>
41 : )
42 : </cfquery>


this is what i was getting before i wrote the code myself, there seem to be a problem with the closing </cfif> tag on line 40 ???


this is the complete code:

<cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
<cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "new_admin_user">
<cfquery datasource="allieddatabase">
INSERT INTO users1 (name, department, username, adminpassword, confirmpassword, level)
VALUES (
<cfif IsDefined("FORM.name") AND #FORM.name# NEQ "">
'#FORM.name#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.department") AND #FORM.department# NEQ "">
'#FORM.department#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.username") AND #FORM.username# NEQ "">
'#FORM.username#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.adminpassword") AND #FORM.adminpassword# NEQ "">
'#FORM.adminpassword#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.confirmpassword") AND #FORM.confirmpassword# NEQ "">
'#FORM.confirmpassword#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.level") AND #FORM.level# NEQ "">
'#FORM.level#'
<cfelse>
NULL
</cfif>
)
</cfquery>
<cflocation url="adminusers.cfm">
</cfif>
Inspiring
March 21, 2007
Dan is right, password is a reserved word in Access.
Of course you need pound signs around your variables.

Regards
Inspiring
March 21, 2007
Looks like you've got a brace "}" instead of close parenthesis ")" here: ..., confirmpassword }

Btw, shouldn't your values list have some pound signs in it ? :)

VALUES ('#form.name#', '#form.department#'..)
Inspiring
March 21, 2007
My guess is that password is a reserved word in access and that this is causing your problems. Try putting square brackets around it.