Tuesday, 29 November 2011

UPDATE RECORD

Once you have a record in your database, chances are you may need to update it at some point. When would you want to do this? Let's say you have a membership based website that is secured with a username and password combination. While you probably would not allow the user to change their username, they certainly can change their password.

To give this option to your members, open your MyData database and create a table called tblUsers with these fields:
ID - autonumber
Usernames - text field
Passwords - text field
DateLastUpdated - date/time field

Next, create a form page called update.asp and copy the below code into your page:



<%
DIM mySQL, objRS
mySQL = "SELECT Username FROM tblUsers"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn
%>

To change your password, enter your Username below:




Username:



As always, there are a lot of ways to collect the username from the user. The main purpose here is to show how to update the record in the database though so on to the good stuff.

Next create a page called update2.asp and copy the below code:

<%
DIM strUsername
strUsername = Request.Form("Username")
IF strUsername <> "" THEN
%>



<%
DIM mySQL, objRS
mySQL = "SELECT * FROM tblUsers WHERE Username = ' " & strUsername & " ' "
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn, adOpenKeyset, adLockPessimistic, adCmdText

IF objRS.EOF THEN
Response.Write "
Sorry, that username does not exist. Please click back on your browser and enter a different username.
"

ELSE
%>



Customer:

Password:



<%
END IF

objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>

<%
ELSE
Response.Write "Please click back on your browser and enter your username."
END IF
%>



Last, create a page called confirm.asp and copy the below code into your page:



<%
DIM mySQL, objRS
mySQL = "SELECT * FROM tblUsers WHERE fUsername = '" & strUsername & "'"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn, adOpenKeyset, adLockPessimistic, adCmdText

objRS.MoveFirst
objRS("Password") = Request.Form("Password")
objRS("DateLastUpdated") = Date()
objRS.Update

objRS.Close
Set objRS = Nothing

Response.Write "
" & strUsername & ",
"
Response.Write " Your password has been succesffully updated in our database.

"

objRS.Close
Set objRS = Nothing
objCONN.Close
Set objCONN = Nothing
%>

That's how you update a record, enjoy.

http://www.aspwebpro.com/aspscripts/records/update.asp

No comments:

Post a Comment