Solved

updating image in database

Posted on 2003-11-18
3
215 Views
Last Modified: 2013-12-24
Hi all

I have a small application that allows the user to create a new record which includes an caption, description and image. I have all this working along with the facility to delete it all. I am trying now to update the records in the database and can update the caption and description fine but the image instead of passing in the new path, sets it to the wwroot directory with a temp file in it.

I have read 2 ways of uploading images. One is to put them in a folder and reference the folder in the database, the other is to upload the image directly (as I am currently doing). I do not know which is best though.

I am using cffile at the moment in the action page but this isnt doing the required action even with the destination set as my desired path.

Thanks in advance
0
Comment
Question by:saffcossie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:saffcossie
ID: 9773317
Does anyone know how to update an image so it overwrites to my chosen path please?
0
 
LVL 17

Accepted Solution

by:
anandkp earned 250 total points
ID: 9776340
The best is to "put images in a folder and reference the folder/file in the database"

with this - its very easy to update/delete image files !

the code for that wld be something like this :
<!---
1. New_FieldName = name of ur file_field on the previous page inside the form tag
2. Image_FieldName = name of the exisiting image
    [used only for deleting previous rec / id can also be used. alter the query below as required]
3. FilePath = full physical path where file needs to be uplaoded OR is uploaded
4. New_ImageName = variable used for assignign new name to the file after uplaod
    [incase of rename option being used]
5. FILEEXT = the extension of the file uploaded - so renaming can be handled properly
6. NewFileName = incase u wanna rename the file- the file uplaoded gets renamed with this value.
 --->

<CFIF ISDEFINED('NEW_FIELDNAME') AND TRIM(NEW_FIELDNAME) NEQ '' AND CGI.CONTENT_LENGTH LT 25000><!--- Upload --->
      <CFIF ACT EQ "EDIT"><!--- Del existing one - only incase of edit mode !!! --->
            <CFQUERY NAME="Del_Img" DATASOURCE="#DSN#" DBTYPE="ODBC" USERNAME="#USERNAME#" PASSWORD="#PASSWORD#">
                  Select New_FieldName from TableName where New_FieldName = #Image_FieldName#
            </CFQUERY>
            <CFIF DEL_IMG.RECORDCOUNT GT 0 AND FILEEXISTS('#FILEPATH##DEL_IMG.NEW_FIELDNAME#')>
                  <CFFILE ACTION="DELETE" FILE="#FilePath##Del_Img.New_FieldName#">
            </CFIF>
            <CFSET NEW_IMAGENAME = ""><!--- Since image is deleted - set it to blank --->
      </CFIF>
      <CFTRY>
          <CFFILE ACTION="UPLOAD" FILEFIELD="New_FieldName" DESTINATION="#FilePath#" NAMECONFLICT="MakeUnique" ACCEPT="image/jpg">
          
            <CFSET NewFileName = "anand"><!--- renaming the uploaded file to anand - change this to whatever in ur code --->
        <CFSET FILEEXT = LISTLAST(FILE.SERVERFILE,'.')><!--- RENAME HERE --->
        <CFFILE ACTION="RENAME" SOURCE="#FilePath##FILE.SERVERFILE#" DESTINATION="#FilePath##NewFileName#.#FILEEXT#">
        <CFSET NEW_IMAGENAME = "#NewFileName#.#FILEEXT#">                      
             
          <CFCATCH>
            <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
            <!--
            alert('image not selected properly pls select the image again')
            history.back(-1)
            //-->
            </SCRIPT>        
        </CFCATCH>
    </CFTRY>
      <CFQUERY NAME="Upd_Table">
            Update Table set ImageName = '#New_ImageName#'
      </CFQUERY>
<CFELSE>
    <!--- Keep the Image As It Is ! --->
    <!--- Dont do ne-thing here --->
</CFIF>

HTH

K'Rgds
Anand
0
 

Author Comment

by:saffcossie
ID: 9776852
Thanks anandkp. Great response. I had something similar but much more basic that wasnt doing it so that has virtually solved my problem so have awarded you the points. I have another question in relation to it so will post it on a new thread so I can award the points.

Cheers
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question