// page information $page_type = "t"; $page_title = "Updating Database Records With ASP"; $page_keywords = "asp, update, records, database, html, code, edit"; $page_description = "Update database records with ASP tutorial. Find more tutorials and scripts at TheScripts.com, a programming and software development resource, directory and community."; $page_articletitle = "Updating Records"; $page_next_url = "/serversidescripting/asp/tutorials/aspanddatabases/page3.html"; $page_next_anchor = "Display Records"; $page_prev_url = "/serversidescripting/asp/tutorials/aspanddatabases/page1.html"; $page_prev_anchor = "Connect to Database"; $page_author = "Robert Murdock"; $page_byline = "Programmer, Darpac Inc."; // site header include ($_SERVER["DOCUMENT_ROOT"]."/header.php"); // begin html ?>
The following example is the code on how to update or edit a record. The code will also check to see if the record already exists. It the record does not already exist, a new record will be added.
File: update.asp
<%
<!--#include file="adovbs.inc"-->
TEMP = Request.form("Name")
Set DataConn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.RecordSet")
DataConn.Open "DBQ=" & Server.Mappath("cdemo.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
' Note: To keep the example simple I update ALL RECORDS with the name that
' is entered in the text box. This is OK for this example since you will not
' (?) allow duplicates. In the real world you would assign a unique ID# to
' each record (access can do this for you).
Set rsInfo = DataConn.Execute("SELECT * FROM tblContact WHERE Name = '" & TEMP & "'")
If rsInfo.EOF Then
SQL = "INSERT INTO tblContact"
SQL = SQL + " (Name, Email, Phone) VALUES ('" & Request("Name") & "', '" & Request("Email") & "', '" & Request.Form("Phone") & "');"
Set rsInfo = DataConn.Execute(SQL)
Response.Write "<CENTER>Record Updated!<br>"
Else
SQL = "UPDATE tblContact"
SQL = SQL + " SET Name = '" & Request("Name") & "', Email= '" & Request("Email") & "', Phone= '" & Request.Form("Phone") & "'"
SQL = SQL + " WHERE Name = '" & TEMP & "';"
Set rsInfo = DataConn.Execute(SQL)
RS.Close
DataConn.Close
Response.Write "<CENTER>Record Updated!<br></CENTER>"
End If
%>