Hi everyone:
I'm trying to determine why Im getting an INSERT error when I run this
ColdFusion page on my server. If anyone would be kind enough as to take a look
at my code and show me what I've done wrong (if anything?) I would appreciate
it. I am running this from a Microsoft Access Database named cfwtf, and the
test table is defined. The test table consists of 2 columns, an autoIncrement
Id, and a text field named 'image'. When ColdFusion tries to execute the
attached code, I continually receive an INSERT error.
When looking at the error I see that it does have the value for image defined
(e.g. when I upload an image named "sometestimage.jpg", the ColdFusion code
spits it back to me in the error statement, and the file gets uploaded in the
correct location). All I'm trying to understand is why it won't copy that file
name into the database when everything else seems to be working correctly.
Thanks in advance!!
<cfscript>
stForm = structNew();
stForm.image = "";
</cfscript>
<cfset myUploadLoc = "C:\inetpub\wwwroot\upload">
<cfif StructKeyExists(form, "btnSubmit")>
<cfscript>
stForm.image = form._image;
</cfscript>
<cftry>
<cffile action="UPLOAD" filefield="image" accept="image/gif, image/jpeg,
image/pjpeg" destination="#myUploadLoc#" nameconflict="MAKEUNIQUE">
<cfcatch type="ANY">
<!-- Catch any potential errors, give a message --->
WTF ARE YOU DOING?!?!
<cfdump var="#cffile#">
<cfabort>
</cfcatch>
</cftry>
<cfquery datasource="cfwtf">
INSERT
INTO test
(image)
VALUES (<cfif
isDefined("cffile.SERVERFILE")>'#cffile.SERVERFILE#'<cfelse>NULL</cfif>)
</cfquery>
</cfif>
<html>
<head>
<title>uploader</title>
</head>
<body>
<table border="0" cellspacing="1" cellpadding="2">
<cfoutput>
<form
name = "aelink"
id = "aelink"
action = "#cgi.script_name#?#cgi.query_string#"
method = "post"
enctype = "multipart/form-data"
>
<input type="hidden" name="processForm" value="1" />
<tr>
<td>Image</td>
<td>
<input type="hidden" name="_image" id="_image" value="#stForm.image#" />
<input type="file" name="image" id="image" size="50" />
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Save"
/>
<input type="button" name="btnCancel" id="btnSubmit" value="Cancel"
onClick="if (confirm('Are you sure you wish to cancel?'))
document.location.href = 'index.cfm'" />
</td>
</tr>
</form>
</cfoutput>
mama_karlo - 06 Jul 2007 20:06 GMT
Can you attach the error message?
Rin- - 06 Jul 2007 23:09 GMT
The error message is as follows:
Error Executing Database Query.
Syntax error in INSERT INTO statement.
42 :
43 : VALUES (
44 : '#cffile.SERVERFILE#'
45 : )
46 :
--------------------------------------------------------------------------------
SQL INSERT INTO test ( image ) VALUES ( 'test.jpg' )
DATASOURCE cfsi
VENDORERRORCODE 3092
SQLSTATE
dempster - 07 Jul 2007 15:26 GMT
I think "image" is a reserved word in the MS Jet SQL engine. Try changing the field name and see what happens.
jdeline - 07 Jul 2007 16:50 GMT
Image is not a reserved word in Access 2002 - see
http://support.microsoft.com/kb/q286335/. Try pulling the CFIF statement out
of the CFQUERY and making the assignment to a temporary variable. Use that
variable in the VALUES clause.
dempster - 07 Jul 2007 18:02 GMT
Image is a reserved word according this documentation:
http://office.microsoft.com/en-us/access/HP010322491033.aspx
The CFIF isn't the problem since the error message that is returned shows the
SQL statement:
INSERT INTO test ( image ) VALUES ( 'test.jpg' )
That 3092 error often indicates a SQL problem usually related to a bad or
invalid field name.
JR "Bob" Dobbs - 07 Jul 2007 23:42 GMT
Try escaping the field name image with brackets and putting the filename value
in a parameter in case the characters in the filename need to be escaped as
well.
INSERT INTO test ( [image] ) VALUES ( <cfqueryparam
value="#cffile.SERVERFILE#" cfsqltype="cf_sql_varchar" /> )
jdeline - 08 Jul 2007 08:56 GMT
So "image" is not a reserved word in Access 2002, but is a reserved word in Access 2003. Which version are you using?
Amit 9808 - 17 Jul 2007 19:18 GMT
Hi,
If the problem still exists then try this code
<cfset thisFile = Evaluate("cffile.SERVERFILE") />
<cfif Len(Trim(thisFile)) >
<cfset filename = #cffile.SERVERFILE#/>
<cfelse>
<cfset filename = " "/>
</cfif>
<cfquery datasource="cfwtf">
INSERT
INTO test
(image)
VALUES
(#filename#)
</cfquery>
Hope this will help you.
Cheers
Amit Rana
cf_dev2 - 17 Jul 2007 20:00 GMT
[q][i]Originally posted by: [b][b]Amit 9808[/b][/b][/i]
<cfset thisFile = Evaluate("cffile.SERVERFILE") />
<cfif Len(Trim(thisFile)) >
<cfset filename = #cffile.SERVERFILE#/>
<cfelse>
<cfset filename = " "/>
</cfif>
[/q]
I don't think Evaulate() is needed here. The error is most likely what [b]
dempster[/b] mentioned: image is a reserved word. They just need to check
the form field and if its not empty perform the upload and insert.
Psuedo-code
<cftry>
<cffile action="upload" ...>
<!--- JR "Bob" Dobbs code --->
<cfquery ....>
INSERT INTO test ( [image] )
VALUES ( <cfqueryparam value="#cffile.SERVERFILE#"
cfsqltype="cf_sql_varchar" /> )
</cfquery>
<cfcatch ..>
Unable to upload file
</cfcatch>
</cftry>
Amit 9808 - 18 Jul 2007 19:22 GMT
I agree with u that Evaluate() is not required here, the value of the
cffile.serverfile can be assigned to a variable without using it.
But the thing which needs to concentrate is that i am using
<cfif Len(Trim(thisFile)) >
for checking the length of the value.
as i think its better to use this rather than using <cfif isDefined()>
because if you just click upload button without selecting any file ( In this
case he is selecting the file but suppose) then sometimes you will get error.
Just try it once.
Please correct me if i am wrong
thanks
cf_dev2 - 18 Jul 2007 20:16 GMT
Agreed. IsDefined() won't work because the file field will always be defined.
I should have been more specific. When I said "check the form field and if
its not empty perform the upload and insert" I meant use your len(trim(...))
check on the form field.
<!--- If a file was uploaded --->
<cfif len(trim(form.theFileField)) gt 0>
.... do the upload
</cfif>
Though its interesting to note that if you enter type anything in the file
field (like 5 space characters) the #len(trim(form.theFileField))# value will
be greater than 0. So it will pass the CFIF test, but CF will throw an error
when you try and upload with cffile.
Dinghus - 18 Jul 2007 23:02 GMT
Hey. How about the simple fact that "image" is not defined as a variable prior
to that. stForm.image is what you want in there.
Even if image is not a reserved word, it is best NOT to use it as a variable
name because it MIGHT cause problems. But I bet if you use the entire variable
name here (stForm.image) it will work.
cf_dev2 - 19 Jul 2007 13:59 GMT
[q][i]Originally posted by: [b][b]Dinghus[/b][/b][/i]
Hey. How about the simple fact that "image" is not defined as a variable prior
to that. stForm.image is what you want in there.
[/q]
No.
Look at the error message and query they posted. Its a SQL error. Since
their code produced a valid sql statement the error has nothing to do with the
stForm variable. The problem is that "Image" is a reserved word. They must
either rename the column or use square brackets around it in their query:
INSERT INTO test ( [b][Image][/b] ) VALUES ( 'test.jpg' )
I don't know what the purpose of stForm.Image is (or if its even needed). But
any other issues with that variable are secondary. The primary problem is
they've used a reserved word as a table column name.
kkchan - 29 Jul 2007 12:30 GMT
If you are using the MS Access driver with unicode support, try the MS Access
driver without unicode support. I get the same error code in an insert
statement in CFMX 6.1. Switching to the MS Access driver without unicode
support resolved the issue for me.
Good luck.
dempster - 29 Jul 2007 16:47 GMT
It gets back to the issue of "image" being a reserved word. Switching drivers
would work (the other Access driver isn't as strict with things like field
lengths and reserved words) but it would be better practice to change the field
name.