ADO Queries and Working with Recordsets - How to Add a Record to Table
(Page 4 of 5 )
You can add records to a database using the INSERT INTO function from our dear friend SQL. The first step is to create an HTML form for the user to submit data to us:
<html>
<body>
<p>Enter your data and click the Submit button to add a record to the database:</p>
<form method=”post” action=”sampleform.asp”>
<table>
<tr>
<td>Victim Name</td>
<td><input name=”victim”></td>
</tr>
<tr>
<td>Victim Injury</td>
<td><input name-”injury”></td>
</tr>
<tr>
<td>How It Happened</td>
<td><input name=”how”></td>
</tr>
</table>
<br /><br /><br />
<input type=”submit”>
</form>
</body>
</html>
Next we must make an .asp page that will process the data:
<html>
<body>
<%
set conn=Server.CreateObject(“ADODB.Connection”)
conn.Provider=”Microsoft.Jet.OLEDB.4.0”
conn.Open “c:/website/chucknorrisvictims.mdb”
sql=”INSERT INTO Victims (VictimName,VictimInjury)”
sql=sql & “ VALUES “
sql=sql & “('” & Request.Form(“victim”) & “',”
sql=sql & “'” & Request.Form(“injury”) & “',”
sql=sql & “'” & Request.Form(“how”) & “')”
on error resume next
conn.Execute sql,recaffected
if err<>0 then
Response.Write(“You do not have permission to update this database”)
else
Response.Write(“<h1>” & recaffected & “has been added</h1>”)
end if
conn.close
%>
</body>
</html>
Next: How To Delete a Record >>
More BrainDump Articles
More By James Payne