Updating database records is a very common process that any typical ASP web application would include. In this tutorial, we will take a look at some sample ADO/ASP codes that can be used to update records in the database.
SQL UPDATE Statement
Structured Query Language also known as ‘SQL‘ is the standard language used with most modern database applications. SQL provides us with a mechanism to select, insert, update and modify records in a database.
In this summary, we are going to focus on the SQL UPDATE statement. For more in-depth information about SQL, please visit the SQL section of this site. There are dozens of tutorials to help you better understand the SQL language.
Syntax
UPDATE tableName SET fieldName=‘value’ WHERE [fieldName] = ‘value’
Examples
In this tutorial, we will take a look at two ASP pages. The first example is an ASP page that a user visits to display the records from a database. A user can click on the button for the employee record that they wish to update. Here is the HTML code for the first page.
Employees
" & UCase(x.name) & " | ") next %>
---|
When the user clicks on the button in the “ID” field, the user will be taken to a page called employeeUpdate.asp. The following example includes the ASP/ADO code that is needed to present the user with the required input elements so that the information about the specific record can be updated in the database record.
If Request.Form("empName")="" then
Set oRs=Server.CreateObject("ADODB.Recordset")
oRs.open "SELECT * FROM employees WHERE id ='" & empID & "'",oConn
%>
<form method="post" action="employeeUpdate.asp">
<table>
<%for each x in oRs.Fields%>
<tr>
<td><%=x.name%></td>
<td><input name="<%=x.name%>" value="<%=x.value%>"></td>
</tr>
<%next%>
</table>
<br /><br />
<input type="submit" class="btn" value="Update record">
</form>
<%
Else
sql="UPDATE employees SET "
sql=sql & "empName ='" & Request.Form("empName") & "',"
sql=sql & "empTitle ='" & Request.Form("empTitle") & "'"
sql=sql & " WHERE id ='" & empID & "'"
on error resume next
oConn.Execute sql
If err<>0 then
Response.Write("Error updating Record!")
Else
Response.Write("Record " & empID & " was updated!")
End If
End if
oConn.close
%>