ASP – An open application environment, it enables powerful server-side scripting.  Home Web Programming ASP Inserting Rows into a Database
Your Ad Here

Inserting Rows into a Database


Inserting Rows into a DatabaseThis 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.

Author's URL: Wilfried Schobeiri
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
Share print this page subscribe to newsletter subscribe to rss

ASP – An open application environment, it enables powerful server-side scripting. More Web Programming: Most Popular Materials | Fresh Materials | More ASP Tutorials at LearnPHP.org

No comments yet...
Add comments to "Inserting Rows into a Database"

Captcha