Print Page | Close Window

UPDATE Sql problem

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=773
Printed Date: 29 March 2026 at 2:55am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: UPDATE Sql problem
Posted By: justinp
Subject: UPDATE Sql problem
Date Posted: 06 March 2003 at 9:51am

Hi.

I was wondering if anyone can help me. I'm using the code below to update records in a DB. The data comes from a form that the user submits. When editing, if a user uploads a new image via the form, the code checks the DB, if the image uploaded is different to the filename in the DB, then the image file referenced in the DB is deleted and the new filename is updated in the DB.

The problem I'm getting is that it seems to be updating the DB entry before it even gets to this line:
If Request.form("frmImageHid") <> ladoImage.fields("itmImage") then 

The other part of the code is used to rename the image depending on other information used in the form. I've tested the code to see how far it gets before it changes the DB. 

I used these lines for testing:

  'response.write "FIELD: "  & Request.form("frmImageHid") & "<br><br>"
  'response.write "DB ENTRY: "  & ladoImage.fields("itmImage") & "<br><br>"

When the lines above are run the DB has already updated, this is before I've run the UPDATE SQL at the bottom.

I'm lost as to why it does this, has anyone any ideas by looking at my code??

Many Thanks

JP

<%
 Case "Update"
 
  strSQL = "SELECT itmImage FROM Items WHERE id=" & request.Querystring("id") & ""
  
  Set ladoImage = ladoConn.Execute(strSQL)
  'response.write "FIELD: "  & Request.form("frmImageHid") & "<br><br>"
  'response.write "DB ENTRY: "  & ladoImage.fields("itmImage") & "<br><br>"
  
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  
  If Request.form("frmImageHid") <> ladoImage.fields("itmImage") then 
   
   'Set path to image
   strPath=server.MapPath("\images\uploaded\") & "\"
   
   strFileName = strPath & ladoImage.fields("itmImage")
  
   Set strFinalStep = objFSO.GetFile(strFileName)
   
   'Delete Image
   strFinalStep.Delete
   
   strItmName=Request.form("frmItmName")
   
   'Request Image name
   strOldFileName= strPath & Request.form("frmImageHid")
   strPartName=replace(strItmName, " ", "_") & ".gif"
   strNewFileName= strPath & strPartName
   
   'Response.write "NOT EQUAL TO<br><br>"
   'Response.write "Old File Name:" & strOldFileName & "<br><br>"
   'Response.write "New File Name:" & strNewFileName & "<br><br>"
   
   If objFso.fileExists(strNewFileName)=false Then
    objFso.MoveFile strOldFileName,strNewFileName
   Else
    Response.write("&nbsp;&nbsp;<br><br>File already Exists")
   End If
   
   strDateTime = FormatDateTime(now(),1) & " " & FormatDateTime(now(),3)
   strInsertSQL = "UPDATE Items SET itmImage='" & strPartName & "', itmName='" & Request.form("frmItmName") & "', itmCat='" & Request.form("itmCat") & "', itmDate='" & strDateTime & "' WHERE id=" & request.Querystring("id") & ""
  Else
  
   'Set path to image
   strPath=server.MapPath("\images\uploaded\") & "\"
   
   strItmName=Request.form("frmItmName")
   
   'Request Image name
   strOldFileName= strPath & Request.form("frmImageHid")
   strPartName=replace(strItmName, " ", "_") & ".gif"
   strNewFileName= strPath & strPartName
   
   'Response.write "EQUAL TO<br><br>"
   'Response.write "Old File Name:" & strOldFileName & "<br><br>"
   'Response.write "New File Name:" & strNewFileName & "<br><br>"
   
   If objFso.fileExists(strNewFileName)=false Then
    objFso.MoveFile strOldFileName,strNewFileName
   End If
   
   strDateTime = FormatDateTime(now(),1) & " " & FormatDateTime(now(),3)
   strInsertSQL = "UPDATE Items SET itmImage='" & strPartName & "', itmName='" & Request.form("frmItmName") & "', itmCat='" & Request.form("itmCat") & "', itmDate='" & strDateTime & "' WHERE id=" & request.Querystring("id") & ""
   
  End If
  
  ladoConn.Execute(strInsertSQL)
  ladoConn.close
%>




Replies:
Posted By: MorningZ
Date Posted: 06 March 2003 at 11:01am
Originally posted by justinp justinp wrote:


The problem I'm getting is that it seems to be updating the DB entry before it even gets to this line:
If Request.form("frmImageHid") <> ladoImage.fields("itmImage") then

why were you expecting different behavior?

Your code:
strSQL = "SELECT itmImage FROM Items WHERE id=" & request.Querystring("id") & ""

Set ladoImage = ladoConn.Execute(strSQL) 'This runs!
'response.write "FIELD: " & Request.form("frmImageHid") & "

"
'response.write "DB ENTRY: " & ladoImage.fields("itmImage") & "

"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Request.form("frmImageHid") <> ladoImage.fields("itmImage") then 'Happens after the fact


-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: justinp
Date Posted: 06 March 2003 at 11:08am

That's only a SELECT statement, that is what selects the image filename from the DB in order to use it on the If Request.form("frmImageHid") <> ladoImage.fields("itmImage") then

For some reason the DB seems to be running the UPDATE SQL at the bottom before it gets to SELECT at the top, don't ask me how.




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net