Inserting Rows into a Database
This tutorial will show you how to insert a row of
data into a database using ASP and an MS Access database.
You can do it on one ASP file, but we'll have two
files: one processor page (proc.asp), and one form
page (form.asp). Then, we'll make the MS Access database
that corresponds to the ASP.
PART I - CREATING THE PROCESSING
PAGE: proc.asp
This page will catch the form data, open a connection
with the Access database (change "test.mdb"
to your database), insert the row using a sql query,
and close the connection. If it executes fine, it will
print out "done inserting row".
Here's the code:
<%
Dim Conn
Dim SqlTemp
If request("insrow") = "1"
THEN
psubject = request("pSubj")
pdate = request("pDate")
ptext = request("pText")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Server.MapPath("\test.mdb")&
";"
Conn.Open
SQLTemp = "INSERT INTO tblTest (pSubj,
pDate, pText) VALUES ('" & psubject
& "', " & pdate & ",
'" & ptext & "')"
Conn.execute(SQLTemp)
Conn.Close
Set Conn = Nothing
response.write("<p>done inserting
row</p>")
End If
%> |
PART II - CREATING THE FORM: form.asp
Here's a simple form that will use the "post"
method to submit the info to the proc.asp page:
<form name="frmInsert"
method="post" action="test.asp">
<input type="hidden" name="insrow"
value="1">
Subject: <input type="text" name="pSubj"
size="13"><br>
Date: <input type="text" name="pDate"
size="13"><br>
Text: <input type="text" name="pText"
size="13"><br>
<input type="submit" size=8 value="Submit">
</form> |
PART III - CREATING THE DATABASE: test.mdb
Make a table in the database called "tblTest"
(you may change it, but make sure it corresponds with
the above table name). Inside the database, make an
Autonumber row called "pSubj" which is the
Primary Key, a date row called "pDate", and
a row called "pText" with the memo row-type.
And there you have it, a database, a form, and a processor
ready for data entry!
NOTE: Had trouble connecting
to the database? Click here.
|