Tuesday, 29 November 2011

INSERT NEW RECORD

There are a lot of instances where you may need to add new records to your database. For example, it is always a good idea to have your website contact form be recorded in your database for future reference.

To do this, you first need to create a database called MyData. Then create a table called tblContact with these fields:
ID - autonumber
Email - text field
FirstName - text field
LastName - text field
Comments - memo field
DateContacted - date/time field

Next, create a form page called form.asp and copy the below code into your page:









Email:
First Name:
Last Name:
Comments:



Then, create a page called confirm.asp and copy the below code into your page:



<%
DIM objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "tblContact", objConn, , adLockOptimistic, adCmdTable

objRS.AddNew
objRS("Email") = Request.Form("Email")
objRS("FirstName") = Request.Form("FirstName")
objRS("LastName") = Request.Form("LastName")
objRS("Comments") = Request.Form("Comments")
objRS("DateContacted") = Date()
objRS.Update
%>

<%
DIM strFirstName
strFirstName = Request.Form("FirstName")
%>,


<%
objRS.Close
Set objRS = Nothing
objCONN.Close
Set objCONN = Nothing
%>

That's all there is to it. Now you can add records to your database.

http://www.aspwebpro.com/aspscripts/records/insertnew.asp

No comments:

Post a Comment