How to update a database field without actually deleting the whole record
I wanted to build a page where people can insert JUST ONE picture in the profile and would need to delete the file before actually updating the database field (setting the value to ""). The name of the database is table is Members, the filed I want to update is img. I implemented some of the codes I got from an article at http://stardeveloper.com
The Pages
editProfile.asp >>>>>>>> profile page that would send the image string (img) to deleteUserPicture.asp to be updated and to delete the file associated to the field
deleteUserPicture.asp >>>>>>>>> performs the action (requires the string img) before it can update it.
Open your access and create a table. Name it Members and create a field with the name img.
Now in editprofile.asp, create a recordset with the name rsMembers and select every column. Create text like "delete your picture." Highlight it and select "Go To Detail Page" from the Server Behaviors menu and select the deleteUserPicture.asp and set the Pass URL = img, recordset = rsMembers, Column = img then click OK. Save the page
Explanation:
rsMembers = name of the recordset you want to update
This page sends a string ("img") to deleteUserPicture.asp
Now open deleteUserPicture.asp and paste the following
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/cnAdmMemNewPicSpon.asp" --> '--------------connection file for the Members table. Your would definitely be different
<%
If(Request.QueryString("img") <> "") Then cmPicture__zero = Request.QueryString("img")
%> '---------receives the string from editProfile.asp
<%
Dim rsdel__idq
rsdel__idq = "1"
If (Request.QueryString("img") <> "") Then rsdel__idq = Request.QueryString("img")
%>
<%
Set rsdel = Server.CreateObject("ADODB.Recordset")
rsdel.ActiveConnection = MM_cnAdmMemNewPicSpon_STRING
rsdel.Source = "SELECT img FROM Members WHERE img = '" + Replace(rsdel__idq, "'", "''") + "'"
rsdel.CursorType = 0
rsdel.CursorLocation = 2
rsdel.LockType = 3
rsdel.Open()
rsdel_numRows = 0
%>
' ----- this is the deletion of the image file ------------
<%
Dim ver
ver = Request.QueryString("img")
If ver <> "" Then
Dim num
Dim xide
Dim whr
Dim sql
Function newFileSystemObject()
Set newFileSystemObject=Server.CreateObject("Scripting.FileSystemObject")
End Function
Function fileExists(aFileSpec)
fileExists=newFileSystemObject.FileExists(aFileSpec)
End Function
' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("pics")
ImagePath = ImagePath & "/" & (rsdel.Fields.Item("img").Value)
' check if file exists and if true delete the file
If fileExists(ImagePath) Then
File.DeleteFile(ImagePath)
End If
%>
'--------------- ends the delete file functions
'--------------------begins the update of the image column
<%
Set cmPicture = Server.CreateObject("ADODB.Command")
cmPicture.ActiveConnection = MM_cnAdmMemNewPicSpon_STRING
cmPicture.CommandType = 1
cmPicture.CommandText = "UPDATE Members SET img = '' WHERE img = '" + Replace(cmPicture__zero, "'", "''") + "' "
cmPicture.Execute()
'---- what the CommandText does is update the Members table by setting the img field to "" and the WHERE img is what filters the recordset.
%>
'-------------------------ends the update of the image column
<% Response.Redirect("editProfile.asp")%>
<%
Else
Response.Write "<span class=""textbold"">No image to delete <a href=""javascript:history.back(1)""</span>Go back</a>"
End If
%>
<%
rsdel.Close()
%>
Contact me at dloverinu@hotmail.com if it doesn't work
Comments
Be the first to write a comment
You must me logged in to write a comment.