Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Add Photo woes..

Status
Not open for further replies.

kelani

MIS
Nov 29, 2000
44
0
0
US
I'm still having trouble with this code! Basically, I'm using CFIF statements to allow a user to upload a photo, and if a photo already exists for that user, delete the existing one and update the database with the new one. Depending on hiw I lay this code out, i get one of two errors.

1. Context validation error in tag CFIF

The tag is not correctly positioned relative to other tags in the template: tag CFIF must have some content. This means that there must be at least one tag, some text, or even just whitespace characters between the <CFIF> and </CFIF> markers

2. The new photo uploads, but the information is not updated, and the old photo is not deleted.

If this is confusing, show me a cfif-cfelse layout that should work, like this

cfif- parameterexists-save

cfif-prepic.recordcount=1
cffifelseif prepic.recordcount=1

</cfif>
<cfelse>
</cfif>

Here we go.


<cfif ParameterExists(Save)>

<cfquery name=&quot;prepic&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot; username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot; dbname=&quot;dbname&quot;>
select id, photo, cap from users WHERE ID='#form.id#'
</cfquery>

<cfif prepic.recordcount IS &quot;1&quot;>

<cffile action=&quot;DELETE&quot; file=&quot;/home/kelani/public_html/lobd9/photos/#photo#&quot;>

<cffile action=&quot;UPLOAD&quot; filefield=&quot;photo&quot; destination=&quot;/home/kelani/public_html/lobd9/photos/&quot; nameconflict=&quot;MAKEUNIQUE&quot; accept=&quot;image/jpeg,image/gif,image/pjpeg&quot; mode=&quot;644&quot;>

<CFIF FILE.FileWasSaved Is True>
<CFSET theRealFileName = FILE.ServerFile>

<cfquery name=&quot;db_insert&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot; username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot; dbname=&quot;dbname&quot;>
UPDATE users SET photo='#theRealFileName#', cap='#cap#' WHERE ID='#form.id#'
</cfquery>
<cfoutput query=&quot;db_find&quot;><a href=&quot; Added!</a> View it now!
</center>
</cfoutput>

<cfelse>

<cffile action=&quot;UPLOAD&quot; filefield=&quot;photo&quot; destination=&quot;/home/kelani/public_html/lobd9/photos/&quot; nameconflict=&quot;MAKEUNIQUE&quot; accept=&quot;image/jpeg,image/gif,image/pjpeg&quot; mode=&quot;644&quot;>

<CFIF FILE.FileWasSaved Is True>
<CFSET theRealFileName = FILE.ServerFile>

<cfquery name=&quot;db_insert&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot; username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot; dbname=&quot;dbname&quot;>
UPDATE users SET photo='#theRealFileName#', cap='#cap#' WHERE ID='#form.id#'
</cfquery>
<cfoutput query=&quot;db_find&quot;><a href=&quot; Added!</a> View it now!
</cfoutput>
</center>

</cfif>

<cfelse>

(standard page shown if no parameters defined)



Thanks, you guys rule!

Kelani
 
Is there an attribute you can set on the <cffile action=&quot;UPLOAD&quot;...> which you use to overwrite any existing file of the same name?? I have not used CF for a while, but when I did, I set this attribute to create a unique name for the file if this was the case, as I did not want to get rid of old versions.
Simon
 
Okay, I've modified the code a bit, but am now getting this problem:

When A user needs to update their photo, I have the prepic query, which should look for an existing phot, and destroy it before adding the new one. The problem is, CF is processing the delete request even if there is no existing photo, which of course returns an error. The same thing happens on a standard &quot;delete your photo&quot; option.

Here's the code once again.

-

<cfif ParameterExists(Save)>

<cfquery name=&quot;prepic&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot; username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot; dbname=&quot;dbname&quot;>
select id, photo, cap from users WHERE ID='#form.id#'
</cfquery>

<cfif prepic.recordcount EQ &quot;1&quot;>
<cfoutput query=&quot;prepic&quot;>
<cffile action=&quot;DELETE&quot; file=&quot;/home/kelani/public_html/lobd9/photos/#photo#&quot;>
</cfoutput>

<cfelse>


<cffile action=&quot;UPLOAD&quot; filefield=&quot;photo2&quot; destination=&quot;/home/kelani/public_html/lobd9/photos/&quot; nameconflict=&quot;MAKEUNIQUE&quot; accept=&quot;image/jpeg,image/gif,image/pjpeg&quot; mode=&quot;644&quot;>

<CFIF FILE.FileWasSaved Is True>
<CFSET theRealFileName = FILE.ServerFile>

<cfquery name=&quot;insert&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot; username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot; dbname=&quot;dbname&quot;>
UPDATE users SET photo='#theRealFileName#', cap='#cap#' WHERE ID='#id#'</cfquery>
<html>
<head>
<title>You are Picturesque</title>
</head>

<cfinclude template=&quot;l8style.cfm&quot;>

<center>

<cfoutput><a href=&quot; Added!</a> View it now!
</center>
</cfoutput>
</body>
</html>

<cfelse>
There was an error uploading your file.
</cfif>
</cfif>

<cfelse>

standard &quot;add your photo&quot; code here
 
Grr, guys, this is still not working. Someone help me and I'll marry your sister :)
 
I think the problem is in your select query &quot;prepic&quot;.

Presumably, this is returning a record even when there is no photo physically on the server - the user will exist in the table, even if they do not have a photo.

Try this:

When you delete a photo from the server (CFfile action=&quot;DELETE&quot;...), remove the entry in the photo field (<CFquery .....>UPDATE users SET photo=&quot;&quot; WHERE ID='#form.id#'</CFquery>).

When you add a photo for a user (CFfile action=&quot;UPLOAD&quot;...), add the path to the user's record.

When you check to see if there is a photo for the user, use the query:
<cfquery name=&quot;prepic&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot; username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot; dbname=&quot;dbname&quot;>
select id, photo, cap from users WHERE ID='#form.id#' AND photo<>''
</cfquery>

(you may also have to check for Null in the field - AND Len(photo &amp; '')<>0 instead of AND photo<>'')
Simon
 
I think this code will do what you want. Simon is right in that you're assuming that they have a picture needing deletion if they are in the table. If this is how your application works, then no problem. If they can be in the table without having a picture uploaded then you want to check to see if the query contains a valid filename instead of checking to see if they're in the table. All I did was move the </cfif> for the &quot;<cfif prepic.recordcount EQ &quot;1&quot;>&quot; statement to just after the delete section. You had it near the bottom which is what I assume was causing your code not to work the way you want. If this works, let me know and I can make a few suggestions on how to improve this as I've coded this same task on several projects.

Good luck,
GJ

<cfif ParameterExists(Save)>

<cfquery name=&quot;prepic&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot;
username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot;
dbname=&quot;dbname&quot;>
select id, photo, cap from users WHERE ID='#form.id#'
</cfquery>

<cfif prepic.recordcount EQ &quot;1&quot;>
<cfoutput query=&quot;prepic&quot;>
<cffile action=&quot;DELETE&quot;
file=&quot;/home/kelani/public_html/lobd9/photos/#photo#&quot;>
</cfoutput>

</cfif>


<cffile action=&quot;UPLOAD&quot; filefield=&quot;photo2&quot;
destination=&quot;/home/kelani/public_html/lobd9/photos/&quot;
nameconflict=&quot;MAKEUNIQUE&quot;
accept=&quot;image/jpeg,image/gif,image/pjpeg&quot;
mode=&quot;644&quot;>

<CFIF FILE.FileWasSaved Is True>
<CFSET theRealFileName = FILE.ServerFile>

<cfquery name=&quot;insert&quot; datasource=&quot;dbname&quot; dbtype=&quot;ODBC&quot;
username=&quot;usname&quot; password=&quot;pswd&quot; dbserver=&quot;localhost&quot;
dbname=&quot;dbname&quot;>
UPDATE users SET photo='#theRealFileName#', cap='#cap#' WHERE ID='#id#'</cfquery>
<html>
<head>
<title>You are Picturesque</title>
</head>

<cfinclude template=&quot;l8style.cfm&quot;>

<center>

<cfoutput><a href=&quot; Photo Added!</a> View it now!
</center>
</cfoutput>
</body>
</html>

<cfelse>
There was an error uploading your file.
</cfif>

<cfelse>
Other code here.
</cfif>
 
Thanks Simon, this works perfectly. Now I just have 2 more minor issues to work out.

1. When performing the update during testing, I need to have a setting in case the database references a file that doesn't exist.

<cfif prepic.recordcount EQ &quot;1&quot;>
<cfoutput query=&quot;prepic&quot;>
<cffile action=&quot;DELETE&quot; file=&quot;/home/kelani/public_html/lobd9/photos/#photo#&quot;>
</cfoutput>
</cfif>

So, if #photo# doesn't exist, it won't return an error. I've looked for <cfif FileExists> or similar functions, but found nothing.

Second, when I try to add a Delete option, I'm getting the same problem:

&quot;Context validation error in tag CFIF

The tag is not correctly positioned relative to other tags in the template: tag CFIF must have some content. This means that there must be at least one tag, some text, or even just whitespace characters between the <CFIF> and </CFIF> markers.&quot;

The code is:

<cfif ParameterExists(Delete)>


<cfquery name=&quot;prepic&quot; datasource=&quot;db26401a&quot; dbtype=&quot;ODBC&quot; username=&quot;us26401a&quot; password=&quot;dxm714&quot; dbserver=&quot;localhost&quot; dbname=&quot;db26401a&quot;>
UPDATE users SET photo='', cap='' WHERE ID='#form.id#'
</cfquery>


<cfoutput query=&quot;prepic&quot;>
<cffile action=&quot;DELETE&quot; file=&quot;/home/kelani/public_html/lobd9/photos/#photo#&quot;>
</cfoutput>

Your photo has been killed
</cfif>

<cfif ParameterExists(Save)>

same code as above..

Looks like I need a tutorial in adding lots of CFIF/ELSEIF statements, but Allaire's no help. Anyone have any easy-to-remember ideas for this? I have a login screen that has hundreds of them working fine, but for some reason, the logic has escaped me :(
 
1. -
You should not get this error if you always delete the reference to the file from the database whenever you delete a file - the query will return no records, and so the <CFfile action=&quot;DELETE&quot;...> line will not be executed.
Saying that, I understand your concern about testing to see if the file exists before deleting it to prevent errors. Unfortunately, it has been 6 months since I coded in ColdFusion and cannot remember any functions to do this (and don't have the help files anymore either).

2. -
There does not seem to be anything wrong with the snippet of code you pasted in your last post. I did notice in the past that some of the error messages were misleading, and it may be a problem with some other code in your page (still with <cfif).
Simon
 
Grr. Well, I am at a loss. The page works fine, until I add the Delete option, which I renamed &quot;Kill&quot; in case it was conflicting with the cffile-delete elsewhere.


<cfif ParameterExists(Kill)>


<cfquery name=&quot;kill&quot; db info here>
UPDATE users set cap='', photo='' WHERE ID='#form.id#' AND pswd=&quot;#pswd#
</cfquery>

Your photo has been killed

</cfif>

If I get rid of the </cfif> and add a <cfelse> it automatically tries to process the (Kill) no matter which way I go into the page.

I've studied my syntax, which is similar to my login page, and can find nothing out of the ordinary.

Grr!
 
Here's the entire thing, minus the KILL code.


-long code block--


<cfif ParameterExists(Save)>


<cfquery name=&quot;prepic&quot; db info>
select id, photo, cap from users WHERE ID='#form.id#' AND photo<>''
</cfquery>


<cfif prepic.recordcount EQ &quot;1&quot;>
<cfoutput query=&quot;prepic&quot;>
<cffile action=&quot;DELETE&quot; file=&quot;/home/kelani/public_html/lobd9/photos/#photo#&quot;>
</cfoutput>
</cfif>


<cffile action=&quot;UPLOAD&quot; filefield=&quot;photo2&quot; destination=&quot;/home/kelani/public_html/lobd9/photos/&quot; nameconflict=&quot;MAKEUNIQUE&quot; accept=&quot;image/jpeg,image/gif,image/pjpeg&quot; mode=&quot;644&quot;>

<CFIF FILE.FileWasSaved Is True>
<CFSET theRealFileName = FILE.ServerFile>

<cfquery name=&quot;insert&quot; db info>
UPDATE users SET photo='#theRealFileName#', cap='#cap#' WHERE ID='#id#'</cfquery>
<html>
<head>
<title>You are Picturesque</title>
</head>

<cfinclude template=&quot;l8style.cfm&quot;>

<center>
<P>
<br><br>
<cfoutput><a href=&quot; Added!</b></a> View it now!
</center>
</cfoutput>
</body>
</html>

<cfelse>
There was an error uploading your file. Please try again.
<P>
<input type=&quot;Button&quot; value=&quot;Go Back&quot; OnClick=&quot;javascript:history.go(-1)&quot; onMouseOver=&quot;window.status='Return to the Login Screen'; return true&quot;>
</P>
</cfif>

<cfelse>

<cfquery name=&quot;getpic&quot; db info>
select photo from users WHERE ID='#id#' AND pswd=&quot;#pswd#&quot;
</cfquery>

<cfif getpic.recordcount EQ &quot;0&quot;>
<html>
<head>
<title>Add your Photo</title>
</head>

<cfinclude template=&quot;l8style.cfm&quot;>
<center>
<b>Your password did not match your User ID</b>
<P>
<input type=&quot;Button&quot; value=&quot;Go Back&quot; OnClick=&quot;javascript:history.go(-1)&quot; onMouseOver=&quot;window.status='Return'; return true&quot;>
</P>
</center>
</body>
</html>

<cfelse>

<html>
<head>
<title>Add your Photo</title>
</head>

<cfinclude template=&quot;l8style.cfm&quot;>

<center>

<table cellspacing=0 cellpadding=5 border=0>
<tr>
<td width=350><img src=&quot;icon.gif&quot; width=78 height=69 alt=&quot;I know you.&quot; border=0 align=&quot;left&quot;>

<font face=&quot;arial,helvetica&quot; size=&quot;-2&quot;>Add your GIF or JPG photo using this form.
<br>
Pictures must be under 75kb, and less than 400 pixels wide or it will be killed.
<br><br>
</font>


<td width=300 align=left bgcolor=&quot;222222&quot;>
<cfform action=&quot;addphoto.cfm?Save=&quot; enctype=&quot;multipart/form-data&quot; method=&quot;post&quot;>
<cfoutput><input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;#id#&quot;></cfoutput>
<font face=&quot;arial,helvetica&quot; size=&quot;-1&quot;><b>Find Pix: &amp;nbsp; </b></font>
<input type=&quot;file&quot; name=&quot;photo2&quot; accept=&quot;image/jpeg,image/gif&quot; maxlength=&quot;255&quot;>
<P>
<font face=arial size=2>Add a caption: (max 200 chars)</font>
<br>
<cfinput type=&quot;Text&quot; name=&quot;cap&quot; value=&quot;&quot; message=&quot;Caption required&quot; required=&quot;Yes&quot; size=&quot;35&quot; maxlength=&quot;200&quot;>
<br>
<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Sexify Yourself&quot;>
</cfform>

</td>
</tr>
<tr>
<td width=&quot;350&quot;><font size=&quot;-1&quot; face=&quot;arial,helvetica&quot;>
Smack <b>browse</b> to locate your pic on your computer, then smack <b>sexify yourself</b> to send it to LOBD9.<br><br>
</font></td>
<td width=&quot;200&quot;>
</td>
</tr>
</td>
</td>

<tr>
<td colspan=2 bgcolor=&quot;222222&quot; align=center><font size=&quot;-2&quot; face=&quot;arial,helvetica&quot;>
<cfif getpic.recordcount eq &quot;1&quot;>
You currently have a picture on file. - <cfoutput><a href=&quot; It</b></a>
</center>
</cfoutput>

<cfelse>
You currently have no picture on file.
</cfif>
</font>
</td>
</td>
</table>
</table>

</body>
</html>
</cfif>

</cfif>
 
Does this page work correctly?? I see only one <table> tag but two </table> tags?? And, before the first </table>, you have two </td> tags??

If it does work correctly and does not work correctly when you add the &quot;kill&quot; section, what does the page look like then??
Simon
 
Simon,

It works correctly as typed.

Quick trivia: I started adding an extra </table> at the end of every page when I was still coding everything in plain HTML and my site grew to 750mb with 59 directories. I'd invariably leave one out for a nested table, and Netscape wouldn't display the page.

I redid things some more, now the Delete option only shows up if there is a pic added. However, when you choose delete, this is what is processed..

<cfif ParameterExists(Kill)>

<cfoutput>
<cffile action=&quot;DELETE&quot; file=&quot;/home/kelani/public_html/lobd9/photos/#form.photo#&quot;>
</cfoutput>

<cfquery name=&quot;killpic&quot; dbinfo>
UPDATE users SET photo='', cap='' WHERE ID='#form.id#' and pswd='#form.pswd#'
</cfquery>


Your Photo Has been Removed

</cfif>


I am getting this error:

Error processing CFFILE

Unable to delete the file '/home/kelani/public_html/lobd9/photos/.' Permission denied

It's chmodded 644, I tried 755, and 777 to see if it was a permission problem. Somehow, the photo field's not being recognized?

The Delete function looks like this:

<cfform action=&quot;addphoto.cfm?Kill=&quot; enctype=&quot;multipart/form-data&quot; method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;#id#&quot;>
<input type=&quot;hidden&quot; name=&quot;pswd&quot; value=&quot;#pswd#&quot;>
<input type=&quot;hidden&quot; name=&quot;photo&quot; value=&quot;#photo#&quot;>
<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Destroy It&quot;>
</cfform>

So, no CFIF errors, just can't get the file removed now. This looks like progress to me. :)

Simon is the man. I owe him lunch.
 
Try two things:

First, in the form that is submitted, check that the #photo# value is being placed into the hidden input type correctly - i.e. output the variable #photo# to the screen (or make the <input type=&quot;hidden&quot; a <input type=&quot;text&quot; for debugging purposes) so that you can check that the value is there to be passed to the &quot;kill&quot; script.

Second, if the first check is succesful, output the variable #photo# to the screen and then do a <cfend> (I think) before the <cfif ParameterExists(Kill)> to see if the photo parameter is being picked up correctly in the &quot;kill&quot; script.
Simon
 
Well, I don't know what I did, but it fixed it. Now the only remaining problem, before I shut up is:

After it processes the delete, It's not stopping at the end of that statement, it's displaying the stuff after the <cfelse> (add a new photo)

I just want it to conirm photo deletion and add a back button.


<cfif ParameterExists(Kill)>

<cfoutput>
<cffile action=&quot;DELETE&quot; file=&quot;/home/kelani/public_html/lobd9/photos/#form.photo#&quot;>
</cfoutput>

<cfquery name=&quot;killpic&quot; dbname>
UPDATE users SET photo='', cap='' WHERE ID='#form.id#' and pswd='#form.pswd#'
</cfquery>


Your Photo Has been Removed

</cfif>

should stop here, but it continues on..


<cfelse>

<html>
<head>
<title>Add your Photo</title>
</head>

<cfinclude template=&quot;l8style.cfm&quot;>

<center>

etc.. read previous code.


This is not a 'critical' problem, at least it WORKS now. I do owe you guys a great deal of gratitude. Anyone for spicy chicken? :)
 
If this is right at the top of your page, you need to remove the </cfif> before your comment of should stop here, but it continues on...

If it is not, then try adding a <cfend> or <cfquit> or <cfabort> - I CANNOT REMEMBER WHICH IS THE CORRECT ONE - which will cease output (so you need your back button before this statement.
Simon
 
Adding <cfabort></cfabort> before the </cfif> Works perfectly.

I thank you all for your most wonderful help in resolving this problem :)

Kelani
 
If I were you I would not leave the page like this. I know you are probably sick of it by now, but relying on a <cfabort> tag is not good practice. If you send me the entire page I will look at it to see if I can spot what can be done to get the page to work correctly without the <cfabort> tag.

PS, you don't need the </cfabort> 0 <cfabort> stops all further processing of tags, so the </cfabort> doesn't even get processed!!
Simon
 
Simon,

Thanks for your time and caring. Send me your email address to kelani@kelani.com and I'll send you a copy of the page. Beats posting it here and taking up bandwidth :)

Kelani
 
Actually, I fixed it. By removing the Cfabort and cfif, then making the next parameter (save) a cfelseif, it appears to be working properly.

Funny, it didn't before. Hmm.. gremlins.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top