Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Problems with cffile

Contributor ,
Dec 03, 2007 Dec 03, 2007
I am trying to write code that will upload a file and add the name of the file uploaded to my DB. I have it on a page that serves 2 functions.

1. add a new record
2. edit an existing record

I am attaching my code here:

I have 3 issues happening here that I don't understand.

1. right now it uploads my picture but doesn't save any information into the body section of the database and doesn't place the image name into the database.

2. on the edit page where I am using the input for the file, I want it to state the file loaded in the DB under it when you go to edit a record.

3. I also get an error when I try and use the accept="image/gif, image/jpeg" portion of the code from the action scrpit.

Can anyone help me? I am stumped and I need to get this working.

Thank you.

Phoenix

2.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 07, 2007 Dec 07, 2007
Great!

now, for the file deletion - i can't really see anything obviously wrong
with it...
one thing i would try is move the cffile action=delete to before the
delete query...
and you can add some debugging to it to try and see what is happening there:

1) add <cfoutput>#pathtofile#</cfoutput> after setting that var to see
what it is being set to
2) put the cffile code inside a cftry/cfcatch block and cfdump the
#cfcatch# variable on error:
<cftry>
...your cffile delete code
<cfcatch type="...
Translate
Participant ,
Dec 03, 2007 Dec 03, 2007
What cf version are you using? ParameterExists() is already deprecated in mx versions. cfmx will not evaluate that function. Try using IsDefined() instead.

example:
<cfif IsDefined("URL.RecordID")>
<!--- your codes --->
</cfif>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 03, 2007 Dec 03, 2007
I changed all those to <cfif IsDefined("URL.RecordID")> now. I am down to error control, and I need to change my code on my update query so that if someone wants to upload a new image, my old image will be delete, and the new one replaced. Can you help me? here is my action code so far:

a;sp. roght now when it writes this to the DB, it is using the absolute path for the image uploaded, how would I change that when I show it on the web page? I don't want someone knowing the true path of the web site.

So right now I am not sure what I am missing and if my code is right as is so far.

Thanks

Phoenix

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 04, 2007 Dec 04, 2007
before we go any further, your form is missing a required enctype to
upload files.

now, for your questions:

Irish-Phoenix wrote:
> I am trying to write code that will upload a file and add the name of the file
> uploaded to my DB. I have it on a page that serves 2 functions.
>
> 1. add a new record
> 2. edit an existing record
>
> I am attaching my code here:
>
> I have 3 issues happening here that I don't understand.
>
> 1. right now it uploads my picture but doesn't save any information into the
> body section of the database and doesn't place the image name into the database.
>

not inserting image name into db: that's because you are trying to
insert '#form.image#' into your table instead of '#cffile.serverfile#'
updating: will not work with your silly <cfupdate>. write a proper
UPDATE query.
updating: you may want to also delete the old file IF a new one has been
uploaded - store current file name in a hidden field in your form, if
len(form.image) gt 0 [=a new file is being uploaded], after successful
cffile action="upload" do a fileexists() on the old file and if it does
cffile action="delete" it

> 2. on the edit page where I am using the input for the file, I want it to
> state the file loaded in the DB under it when you go to edit a record.
>

the code you have for it now is totally wrong. since you are (or rather
will be) storing just a pointer to the image file in your db (i.e. just
the image filename), to then display it on your page you need an <img
...> tag. the src attribute of it will have path to the image folder (as
a variable or hard-coded) and the value of the #image# field from your
table, i.e. <img src="images/#image# alt="">

> 3. I also get an error when I try and use the accept="image/gif, image/jpeg"
> portion of the code from the action scrpit.
>

what error do you get exactly? which browser are you using for testing
(you should be using at least 3 different ones). in some browsers, on
some computers, MIME types handling may be setup differently. my IE7
always uploads .jpg files as image/pjpeg - if you do not have that type
in the accept attribute the file will not be accepted and will throw an
error

> Can anyone help me? I am stumped and I need to get this working.
>
> Thank you.
>
> Phoenix

hth

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 04, 2007 Dec 04, 2007
I fixed all those issues. I will attach my edit page code. and my cfupdate is now a query. (I changed that also, cfupdate code line stinks, a query works better. now I am down to the following issues, otherwise I have everything fixed that you wrote about:

I am down to error control, and I need to change my code on my update query so that if someone wants to upload a new image, my old image will be delete, and the new one replaced.

as of right now when it writes this to the DB, it is using the absolute path for the image uploaded, how would I change that when I show it on the web page? I don't want someone knowing the true path of the web site.

At this point everything works, accept for the issues posted here. It is my action script that needs help now. the edit page is good to go.

Here is my code:

Thanks for the help

Phoenix

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 04, 2007 Dec 04, 2007
1) on your Action.cfm page i would surround <cfile action="upload" ...> with
<cfif len(trim(form.myfile))>
...
</cfif>
this will make sure no upload is attempted if no file has been selected

2) in your update query:
- replace " around form.title with '
- add # around form.title
- use <cfqueryparam ...> on all values!!!!

3) use <cfqueryparam on all values in your INSERT query, too!!!

4) if i were you, i would change your logic a bit and check for
len(form.myfile) and the length of cgi.content_length BEFORE updating
the db. reason: unless you employ some form of user notification, there
si no way for your user to realise now that their file has not been
uploaded because it was too large... i would first check for those
lengths and not perform any updates/inserts and show user an error about
file length and/or type (and other errors like missong required fields,
etc), and only if there were no errors in submitted data i would attempt
insert/update

5) absolutely no need for that cflock and that cftransaction there

6) tthe absolute path you used in the cffile is NOT stored in the db
field. only the filename (which is what #cffile.serverfile# represents)
is stored. i am not even 100% sure you can actually use an absolute web
path in cffile destination=""... one usually uses
#expandpath('relativepathtofolder')#, because, iirc, the desctination
attribute requires a full path to a dir on the server...not a website path..

7) your Image in use: #variables.MYFile# will have to be Image in use:
<img src="rel_or_abs_path_to_img_folder/#variables.MYFile#"> if you want
to show the actual image. if you are just displaying the filename, then
all is good.

8) if you want to delete current image on update (if a new image has
been selected), then you need to store current image filename in a
hidden field in the form, and on the action page check for:
a) if old image existed (len of hidden field)
b) new image selected (len of form.myfile)
c) if b is true check if old file still exists on server (with
fileexists() function)
d) if c is true use cffile action=delete to delete old file
e) upload new file and update db

hth

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 04, 2007 Dec 04, 2007
Let me break this down.... starting with my action page code and an error I am getting at the moment:

Action code:
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg, image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature" nameconflict="MAKEUNIQUE"></cfif>
<cfif IsDefined("Form.RecordID")>
<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
UPDATE feature
SET feature.title="#form.title#", feature.Body='#form.PDSeditor#', feature.MYFile='#cffile.serverfile#'
WHERE feature.ID = #form.ID#
</cfquery>
<cflocation url="feature-manager.cfm">
<cfelse>

<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
INSERT INTO feature (title, Body, MYFile)
VALUES ('#form.title#', '#form.PDSeditor#', <cfif FileExists(form.MYFile)>'#cffile.serverfile#'<cfelse>NULL</cfif>
</cfquery>

<cflocation url="feature-manager.cfm">

Error Message:
Error Diagnostic Information
ODBC Error Code = 07001 (Wrong number of parameters)


[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

Hint: The cause of this error is usually that your query contains a reference to a field which does not exist. You should verify that the fields included in your query exist and that you have specified their names correctly.

SQL = "UPDATE feature SET feature.title="Ornaments", feature.Body='test', feature.MYFile='ornement.jpg' WHERE feature.ID = 0"

Data Source = "CREATIVE"


The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (163:3) to (163:105) in the template file c:\websites\x9vdzd\admin\Action.cfm.


Now one question at a time that you wrote:

1) I fixed that with your code:
<cfif len(trim(form.myfile))>
...
</cfif>

2) is all fixed, found that error this morning

3) How would this be written? <cfqueryparam
Like this? <cfqueryparam feature.title="#form.title#">, ...

4) I don't think I need to check for size on this, it is a thumb nail image only. around the upload area on the edit page I have instructions on the image size. BUT, how would I write this to make it work so if they couldn't read, it would throw an error and take them back to the edit page?

5) took away cflock and so on...

6) I fixed this am made the absolute path.. I have to look in the DB for what it is placing in there now that it is fixed. it does however upload the image to the folder I pointed to. (Even with it throwing the error posted in this message.)

7) in the area I am working now, I only wanted the name of the image, not the actual image. BUT on the front where the public looks at the site, do I use: <img src="rel_or_abs_path_to_img_folder/#variables.MYFile#">?

8) now this one confuses me... I realize I need 2 cffile statements here, one for delete and one for upload. (Right?) I am not sure how to write it out for the upload to look for an existing file, if it exists, delete it and upload a new one. (I want it to do it even if the file name is different, is that possible?)
Would it look like this? (could you help me write it out?

Edit page hidden field: <input name="MYFile" type="hidden" value="#variables.MYFile#">

Action page:

<cfif (len of hidden field=existingFile)>
<cffile action="delete" destination="c:/websites/x9vdzd/img/feature" file="#form.existingFile#">
<cfelse>
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg, image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature" nameconflict="MAKEUNIQUE"></cfif></cfif>

Is this right?

Thanks for the help

Phoenix
Would it go something like this

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 04, 2007 Dec 04, 2007
Irish-Phoenix wrote:
> Error Message:
> Error Diagnostic Information
> ODBC Error Code = 07001 (Wrong number of parameters)
>
>
> [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
>
> Hint: The cause of this error is usually that your query contains a reference
> to a field which does not exist. You should verify that the fields included in
> your query exist and that you have specified their names correctly.
>
> SQL = "UPDATE feature SET feature.title="Ornaments", feature.Body='test',
> feature.MYFile='ornement.jpg' WHERE feature.ID = 0"
>

see my first - in 2) in previous post (hint: single quotes vs. double
quotes)

> 3) How would this be written? <cfqueryparam
> Like this? <cfqueryparam feature.title="#form.title#">, ...

look in cfml reference. if you do not have one - download from adobe.com
(download the developer guide, too, while you are at it. and read it.)

>
> 4) I don't think I need to check for size on this, it is a thumb nail image
> only. around the upload area on the edit page I have instructions on the image
> size. BUT, how would I write this to make it work so if they couldn't read, it
> would throw an error and take them back to the edit page?

i am not sure what you mean... maybe it is the "few" beers i had or
maybe t is you... i am not sure... :)
it's 12:40 AM here and i am not at my best now...

> 6) I fixed this am made the absolute path.. I have to look in the DB for what
> it is placing in there now that it is fixed. it does however upload the image
> to the folder I pointed to. (Even with it throwing the error posted in this
> message.)

of course. cffile and cfquery are 2 different animals. if one fails does
not mean another will, too.
cffile (action=upload) uploads a file from user's computer to a folder
on the server.
cfquery (update/insert query) enters or updates recordsin the db. f
course, the 2 are not related a all.

>
> 7) in the area I am working now, I only wanted the name of the image, not the
> actual image. BUT on the front where the public looks at the site, do I use:
> <img src="rel_or_abs_path_to_img_folder/#variables.MYFile#">?

yes. i think so. if you want to actually display the image on user's screen.
>
> 8) now this one confuses me... I realize I need 2 cffile statements here, one
> for delete and one for upload. (Right?) I am not sure how to write it out for
> the upload to look for an existing file, if it exists, delete it and upload a
> new one. (I want it to do it even if the file name is different, is that
> possible?)
> Would it look like this? (could you help me write it out?
>
> Edit page hidden field: <input name="MYFile" type="hidden"
> value="#variables.MYFile#">

presumed correct.

>
> Action page:
>
> <cfif (len of hidden field=existingFile)>

just len(trim(form.hiddenfieldname)) will suffice.

> <cffile action="delete" destination="c:/websites/x9vdzd/img/feature"
> file="#form.existingFile#">

aha! you are forgetting the fileexists() part. without checking for
existence of specified file in the specified folder you are running a
risk of wiping all files in the folder.... look up fileexists() in the
cfml reference if you are not familiar with it.

> <cfelse>
> <cfif len(trim(form.MYFile))>
> <cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg,
> image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature"
> nameconflict="MAKEUNIQUE"></cfif></cfif>

yep.

>
> Is this right?

with above corrections it should be.


---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 04, 2007 Dec 04, 2007
I did a lot of editing on this now. I also just got back from picking up a book for CF. But I am still having trouble with an error and can't figure out why.

This is what my code is now on the Action Page:

<cfif #FileExists("c:/websites/x9vdzd/img/feature/#MYFile#")#>
<cfif len(trim(form.hiddenfieldname))>
<cffile action="delete" file="#form.hiddenfilename#"></cfif></cfif>
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg, image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature" nameconflict="MAKEUNIQUE"></cfif>
<cfif IsDefined("Form.RecordID")>
<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
UPDATE feature
SET feature.title='#form.title#', feature.Body='#form.PDSeditor#', feature.MYFile='#cffile.serverfile#'
WHERE feature.ID = #form.ID#
</cfquery>
<cflocation url="feature-manager.cfm">
<cfelse>

<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
INSERT INTO feature (title, Body, MYFile)
VALUES ('#form.title#', '#form.PDSeditor#', <cfif FileExists(form.MYFile)>'#cffile.serverfile#'<cfelse>NULL</cfif>
</cfquery>

This is my error:

Error Diagnostic Information
ODBC Error Code = 37000 (Syntax error or access violation)


[Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.


SQL = "INSERT INTO feature (title, Body, MYFile) VALUES ('Ornements', 'test', 'ornement.jpg'"

Data Source = "CREATIVE"


The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (177:3) to (177:105) in the template file c:\websites\x9vdzd\admin\Action.cfm.

Now what am I missing? >.<

Also, is my FileExists and delete function written properly?

Thanks for the help so far.Z

Phoenix
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 04, 2007 Dec 04, 2007
> This is my error:
...
> SQL = "INSERT INTO feature (title, Body, MYFile) VALUES ('Ornements', 'test',
> 'ornement.jpg'"
...
> Now what am I missing? >.<

a ) after the closing </cfif> in your query.

>
> Also, is my FileExists and delete function written properly?

let's see....

> This is what my code is now on the Action Page:
>
> <cfif #FileExists("c:/websites/x9vdzd/img/feature/#MYFile#")#>
> <cfif len(trim(form.hiddenfieldname))>
> <cffile action="delete" file="#form.hiddenfilename#"></cfif></cfif>

no, they are not.
i assume, as in previously posted code, form.MYFile is the file input
field. i also assume form.hiddenfilename is a hidden form field
type-text that contains the name of previously uploaded file (or an
empty string if no file has been previously uploaded).
based on the above assumptions:
1) form.myfile is not a file. see more at the bottom.
2) it is thus wrong doing a fileexists() for #MYFile#
3)you need to check that PREVIOUS file exists (if a file has been
previously uploaded). so you need to expand your second line of code to
include a correct fileexists() check .

> <cfif len(trim(form.MYFile))>
> <cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg,
> image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature"
> nameconflict="MAKEUNIQUE"></cfif>

the above looks syntactically correct

> <cfif IsDefined("Form.RecordID")>
> <cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#"
> password="#sitePassword#">
> UPDATE feature
> SET feature.title='#form.title#', feature.Body='#form.PDSeditor#',
> feature.MYFile='#cffile.serverfile#'
> WHERE feature.ID = #form.ID#
> </cfquery>

now, the above has a logic flaw: what if i am updating a record but i
did not select any file to upload?
your current update query will blow up. hint: update the file record
only if a new file has been uploaded...
pls, of course, USE <CFQUERYPARAM ...>!!!!

> <cflocation url="feature-manager.cfm">
> <cfelse>
>
> <cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#"
> password="#sitePassword#">
> INSERT INTO feature (title, Body, MYFile)
> VALUES ('#form.title#', '#form.PDSeditor#', <cfif
> FileExists(form.MYFile)>'#cffile.serverfile#'<cfelse>NULL</cfif>
> </cfquery>

form.MYFile is not a file. it is only a pointer to the user-selected
file, or rather a temp file created by cf on the server to represent the
user's file. fileexists(form.myfile) is thus not correct. all you
really need to do in the above query is to check if form.myfile is
emprty or not using the same old len(trim()) functions.

and, again, USE <CFQUERYPARAM ...>!!!


and, a general question: why using ODBC connectors? why not JDBC? which
db are you using?






---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 04, 2007 Dec 04, 2007
I am using an access DB.
I think I fixed a lot of this. BUT now for some reason and I can't figure it out, when I go to add a record, it doesn't catch the insert query. >.<

This is my code.

<cfif #FileExists("c:/websites/x9vdzd/img/feature/#MYFile#")#>
<cfif len(trim(form.hiddenfieldname))>
<cffile action="delete" file="#form.hiddenfilename#"></cfif></cfif>
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg, image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature" nameconflict="MAKEUNIQUE"></cfif>
<cfif IsDefined("Form.ID")>
<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
UPDATE feature
SET feature.title='#form.title#', feature.Body='#form.PDSeditor#', feature.MYFile='#cffile.serverfile#'
WHERE ID = #form.ID#
</cfquery>
<cflocation url="feature-manager.cfm">
<cfelse>
<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
INSERT INTO feature (title, Body, MYFile)
VALUES ('#form.title#', '#form.PDSeditor#', <cfif FileExists(form.MYFile)>'#cffile.serverfile#'<cfelse>NULL</cfif>)
</cfquery>

Now I have a question about the CFQUERYPARAM and it's proper use. I was reading, and still am having a hard time figuring out where to use it and it's proper syntax.

Is it used like this?
<cfqueryparam value= "#URL.ID#" cfsqlType= "CF_SQL_INTEGER">

Do I use it like that in my querys in place of the WHERE statement?

But now I think my last bug is this problem with the edit page missing the insert query and I don't know why, I had it working. It does upload an image, not sure if it deletes the image yet, I can find out once I figure out why the insert stopped working.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 04, 2007 Dec 04, 2007
I did some work on the logic. please let me know if I am correct:

<cfif getFileName.recordCount GT 0>
<cfset pathToFile = "c:/websites/x9vdzd/img/feature/#getFileName.MYFile#">
<cfif FileExists(pathToFile)>
<cffile action="delete" file="#pathToFile#"></cfif>
<cfelse>
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg, image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature" nameconflict="MAKEUNIQUE"></cfif>
<cfif IsDefined("Form.ID")>
<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
UPDATE feature
SET feature.title='#form.title#', feature.Body='#form.PDSeditor#', feature.MYFile='#cffile.serverfile#'
WHERE ID = <cfqueryparam value= "#form.ID#" cfsqlType= "CF_SQL_INTEGER">
</cfquery>
<cflocation url="feature-manager.cfm">
<cfelse>
<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
INSERT INTO feature (title, Body, MYFile)
VALUES ('#form.title#', '#form.PDSeditor#', <cfif FileExists(form.MYFile)>'#cffile.serverfile#'<cfelse>NULL</cfif>)
</cfquery>

It still skips over the insert tag for some reason, again, not sure if the delete function works either. I can figure that out once I get it to write a file to the insert again.
2 questions:

1. is my use of crqueryparam correct?
2. on my insert statement, what would I change it to? <cfif FileExists(form.MYFile)> If this is incorrect what would it be?

Also, is my code for checking if the image exists correct? if not, what would I change that 2?

Sorry, been at this for over 10 hrs. @.@ my computer has come very close to flying out the window. (I'm sure you understand) Can you help me write this so it will work? I am totally frustrated at this point. I had it working. (Been reading all night also)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 04, 2007 Dec 04, 2007
did you see my previous post?
you still seem to have all the errors i outlined in your code...

re <cfqueryparam>:
you use it not INSTEAD of the WHERE clause, but IN it.

so if you had
WHERE id = #url.id# AND name = '#form.name#'

you would change that to
WHERE id = <cfqueryparam cfsqltype="cf_sql_integer" value="#url.id#">
AND name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.name#">

there are a lot of blogs/tutorials/forms that explain the benefits of
using it, 2 main ones being a) prevention of sql injections and b) query
optimization.

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 04, 2007 Dec 04, 2007
Irish-Phoenix wrote:
>
> It still skips over the insert tag for some reason, again, not sure if the
> delete function works either. I can figure that out once I get it to write a
> file to the insert again.

it skips because you are telling it to skip!!!:

<cfif getFileName.recordCount GT 0>
...delete file code...
<cfelse>
...update/insert code...
</cfif>

do you see it? the update/insert will only run if a file already exists!

more soon....


---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 05, 2007 Dec 05, 2007
No it was skipping it before I put the code in to delete the file and then upload a new one in it's place.

So the errors are still in my verification code of cfif there is a file, delte it and upload a new one
and in the 2 querys where I need to make conditions trying to find out if the file is in the form, if it is use it, if not, don't. so the update query won't vlow up if there is no file being loaded up in an edit.

Would both querys use the same code in the query?


Can you help me fix this so it will work. I am doing a lot of reading, and right now I am under the gun to get this working right.

I am still stumped on why it is skipping the insert query.

I also changed the file detection for the delete upload to this:

<cfif getFileName.recordCount GT 0>
<cfset pathToFile = "c:/websites/x9vdzd/img/feature/#getFileName.MYFile#">
<cfif FileExists(pathToFile)>
<cffile action="delete" file="#pathToFile#"></cfif></cfif>
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif, image/jpg, image/jpeg, image/pjpeg" destination="c:/websites/x9vdzd/img/feature" nameconflict="MAKEUNIQUE">

But even before I did that code it was skipping the insert:

<cfquery datasource="#sitedatasource#" dbtype="ODBC" username="#siteUserID#" password="#sitePassword#">
INSERT INTO feature (title, Body, MYFile)
VALUES ('#form.title#', '#form.PDSeditor#', <cfif FileExists(form.MYFile)>'#cffile.serverfile#'<cfelse>NULL</cfif>)
</cfquery>

It stopped working for no reason, it was working until I got my delelte record and file code working.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2007 Dec 05, 2007
try this code:

FORM PAGE:

<cfparam name="url.id" type="integer" default="0"
<cfparam name="variables.ID" type="integer" default="#url.id#">
<cfparam name="variables.title" default="">
<cfparam name="variables.body" default="">
<cfparam name="variables.MYFile" default="">

<cfif url.id GT 0>
<cfquery name="featRec" dataSource="#sitedatasource#">
SELECT title, body, MYFile, ID
FROM feature
WHERE ID = <cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
</cfquery>
<!--- if the record was found, store the values --->
<cfif featRec.RecordCount EQ 1>
<cfset variables.ID = featRec.ID>
<cfset variables.title = featRec.title>
<cfset variables.body = featRec.body>
<cfset variables.MYFile = featRec.MYFile>
</cfif>
</cfif>
<head>
</head>
<body>
<form action="Action.cfm" method="post" name="content" id="content"
enctype="multipart/form-data" onsubmit="saveIt()">
<input type="hidden" name="ID" value="#variables.ID#">
<input type="hidden" name="oldimage" value="#variables.MYFile#">

<input type="text" name="title" class="textInputs"
value="#variables.title#" maxLength="510">
<cfif len(trim(variables.MYFile)) GT 0>Image in use: #variables.MYFile#
</cfif>
<input name="MYFile" type="file" id="MYFile">
<textarea cols="" rows="" name="PDSeditor" style="display:
none">#variables.Body#</textarea>

<input type="submit" class="formButtons" name="feat_OK" value=" OK ">
<input type="submit" class="formButtons" name="feat_Cancel" value="Cancel">
</form>



ACTION PAGE:

<cfif isdefined("form.feat_OK")>
<cfparam name="fileuploaded" type="boolean" default="false">
<cfparam name="uploadedfile" default="">
<cfif len(trim(form.oldimage))>
<cfset pathToFile = "c:/websites/x9vdzd/img/feature/#form.oldimage#">
<cfif FileExists(pathToFile)>
<cffile action="delete" file="#pathToFile#">
</cfif>
</cfif>
<!--- --->
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif,
image/jpg, image/jpeg, image/pjpeg"
destination="c:/websites/x9vdzd/img/feature/" nameconflict="MAKEUNIQUE">
<cfset fileuploaded = true>
<cfset uploadedfile = cffile.serverfile>
</cfif>
<cfif IsDefined("Form.ID")>
<cfquery datasource="#sitedatasource#" username="#siteUserID#"
password="#sitePassword#">
UPDATE feature
SET
feature.title=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#form.title#">,
feature.Body=<cfqueryparam cfsqltype="cf_sql_longvarchar"
value="#form.PDSeditor#">,
feature.MYFile=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#uploadedfile#" null="#NOT fileuploaded#">
WHERE ID = <cfqueryparam value="#form.ID#" cfsqlType="CF_SQL_INTEGER">
</cfquery>
<cflocation url="feature-manager.cfm">
<cfelse>
<cfquery datasource="#sitedatasource#" username="#siteUserID#"
password="#sitePassword#">
INSERT INTO feature
(title,
Body,
MYFile)
VALUES
(<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.title#">,
<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.PDSeditor#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#"
null="#NOT fileuploaded#">)
</cfquery>
</cfif>
</cfif>

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 05, 2007 Dec 05, 2007
Thanks for this. (I just woke up. needed some sleep)
It threw an error on the edit page and I changed the param to this

<cfparam name="url.id" type="boolean" default="0">
<cfparam name="variables.ID" type="boolean" default="#url.id#">

and it worked and loaded the page. now when I upload the record I get this error:

Error Diagnostic Information
Just in time compilation error

Invalid parser construct found on line 200 at position 31. ColdFusion was looking at the following text:

NOT
Invalid expression format. The usual cause is an error in the expression structure.
The last successfully parsed CFML construct was a CFQUERYPARAM tag occupying document position (199:16) to (199:28).

The specific sequence of files included or processed is:
c:\websites\x9vdzd\admin\Action.cfm


Do I take the word NOT out of the insert query for the fileupload?
that is the line throwing the error:

feature.MYFile=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#uploadedfile#" null="#NOT fileuploaded#">

that is all so far. Now I understand where to use the <cfqueryparam in the rest of my querys, I was reading about that and saw the cf_sql_varchar but wasn't sure if that was for text imputs to the DB.

Thanks again!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 05, 2007 Dec 05, 2007
It threw an error on the edit page and I changed the param to this

<cfparam name="url.id" type="boolean" default="0">
<cfparam name="variables.ID" type="boolean" default="#url.id#">

and it worked and loaded the page. now when I upload the record I get this error:

Error Diagnostic Information
Just in time compilation error

Invalid parser construct found on line 200 at position 31. ColdFusion was looking at the following text:

NOT
Invalid expression format. The usual cause is an error in the expression structure.
The last successfully parsed CFML construct was a CFQUERYPARAM tag occupying document position (199:16) to (199:28).

The specific sequence of files included or processed is:
c:\websites\x9vdzd\admin\Action.cfm


Do I take the word NOT out of the insert query for the fileupload?
that is the line throwing the error:

feature.MYFile=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#uploadedfile#" null="#NOT fileuploaded#">

Right now it isn't loading up an image to the server and throws this error. Is there a problem in the code? or the server? I also took out the NOT and when I tried to add a new record, it just hung up on the action page. no error, but no image or record.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2007 Dec 05, 2007
hmm... it works fine in my tests...
can you post current code of your action page, please?

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2007 Dec 05, 2007
and if you are on your testing server (not on production/hosting
server), please do turn on the "Enable Robust Exception Information"
under Debugging Settings in CF Administrator - it will give you a lot
more informative errors.

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 05, 2007 Dec 05, 2007
I think I know what the problem is.
1. My delepoment server is only cf 6, (time to upgrade!)
2. The hosting company had the site on cf 5! I have them moving it to a cf 7 server right now. It should be done tomorrow so I can test it on there.

Think that is what the problem was? I do! probably the problem I have been having all along I bet, I'm writting for a newer version of CF and the site is on an antique!

I'll let you know what happens. Thanks for all you help. Took me getting some sleep to figure out it was the servers cf version.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 06, 2007 Dec 06, 2007
Ok, site is on CF 8 server now.

When I post a new record it doesn't do anything, not to the DB no image and hangs up on the action page.

Here is the code, I used it just like you posted it:

Edit page:

<cfparam name="url.id" type="integer" default="0">
<cfparam name="variables.ID" type="integer" default="#url.id#">
<cfparam name="variables.title" default="">
<cfparam name="variables.body" default="">
<cfparam name="variables.MYFile" default="">


<cfif url.id GT 0>
<cfquery name="featRec" dataSource="#sitedatasource#">
SELECT title, body, MYFile, ID
FROM feature
WHERE ID = <cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
</cfquery>
<!--- if the record was found, store the values --->
<cfif featRec.RecordCount EQ 1>
<cfset variables.ID = featRec.ID>
<cfset variables.title = featRec.title>
<cfset variables.body = featRec.body>
<cfset variables.MYFile = featRec.MYFile>
</cfif>
</cfif>
<head>
</head>
<body>
<cfoutput>
<form action="Action.cfm" method="post" name="content" id="content"
enctype="multipart/form-data" onsubmit="saveIt()">
<input type="hidden" name="ID" value="#variables.ID#">
<input type="hidden" name="oldimage" value="#variables.MYFile#">

<input type="text" name="title" class="textInputs"
value="#variables.title#" maxLength="510">
<cfif len(trim(variables.MYFile)) GT 0>Image in use: #variables.MYFile#
</cfif>
<input name="MYFile" type="file" id="MYFile">
<textarea cols="" rows="" name="PDSeditor" style="display:
none">#variables.Body#</textarea>

<input type="submit" class="formButtons" name="feat_OK" value=" OK ">
<input type="submit" class="formButtons" name="feat_Cancel" value="Cancel">
</form>


Action Page:

<cfif isdefined("form.feat_OK")>
<cfparam name="fileuploaded" type="boolean" default="false">
<cfparam name="uploadedfile" default="">
<cfif len(trim(form.oldimage))>
<cfset pathToFile = "c:/websites/x9vdzd/img/feature/#form.oldimage#">
<cfif FileExists(pathToFile)>
<cffile action="delete" file="#pathToFile#">
</cfif>
</cfif>
<!--- --->
<cfif len(trim(form.MYFile))>
<cffile Action="upload" filefield="MYFile" accept="image/gif,
image/jpg, image/jpeg, image/pjpeg"
destination="c:/websites/x9vdzd/img/feature/" nameconflict="MAKEUNIQUE">
<cfset fileuploaded = true>
<cfset uploadedfile = cffile.serverfile>
</cfif>
<cfif IsDefined("Form.ID")>
<cfquery datasource="#sitedatasource#" username="#siteUserID#"
password="#sitePassword#">
UPDATE feature
SET
feature.title=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#form.title#">,
feature.Body=<cfqueryparam cfsqltype="cf_sql_longvarchar"
value="#form.PDSeditor#">,
feature.MYFile=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#uploadedfile#" null="#NOT fileuploaded#">
WHERE ID = <cfqueryparam value="#form.ID#" cfsqlType="CF_SQL_INTEGER">
</cfquery>
<cflocation url="feature-manager.cfm">
<cfelse>
<cfquery datasource="#sitedatasource#" username="#siteUserID#"
password="#sitePassword#">
INSERT INTO feature
(title,
Body,
MYFile)
VALUES
(<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.title#">,
<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.PDSeditor#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#"
null="#NOT fileuploaded#">)
</cfquery>
</cfif>
</cfif>
<cflocation url="feature-manager.cfm">


Can I scream now? I was hoping moving this over would make it work >.<

No errors, just nothing posted and hangs up on Action page.

Sorry.. and thanks for the help!
Any ideas?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 06, 2007 Dec 06, 2007
start by adding <cfdump var="#form#"> at the top of your action page.

in the dump data see if you can spot form.feat_OK with value ' OK '.

i suspect that your saveIt() function that you run on form submission
does something that prevents all form fields from being passed in the
form scope...

if you do not see form.feat_OK in the dump data, then change
<cfif isdefined('form.feat_OK')> on the action page to <cfif
isdefined('form.ID')>

hth

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 06, 2007 Dec 06, 2007
I did the cfdump, and I had all the information in the form return back to me.
once I took it off, it uploaded the image, but didn't save anything to the DB.

The saveit() is a function for my text editor, it has to be there.

No errors, just an image uploaded and no data in the db
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 07, 2007 Dec 07, 2007
argh!
of course! your FORM.ID is ALWAYS defined, since it is set to 0 by
default if it does not exist!
so the action page is always trying to run an update query - thus no
inserts of new records.

change the <cfif isdefined('FORM.ID')> to <cfif form.id gt 0> and it
should work.

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources