Summary:
Databases can be very useful within dynamic websites, you can store all kinds of data and use are used in Guestbooks, Message Boards, Photo Gallerys and countless web applications. But if you cannot add data to your database it becomes rather useless, this tutorial is designed to help you add data into your database to make your website alot more useful for your visitors.

In this tutorial we will use a Guestbook as an example to help demostrate how easy it can be to insert data into a database. We will be using a database called 'tblGuestbook' with the following fields:

  • ID (autonumber)
  • Name (text field)
  • Email (text field)
  • Message (memo field)

HTML Form:
Now we need a HTML form for a visitor to enter their comment for our guestbook. The following will be fine for our example.

<html>
<head>
<title> Guestbook Form </title>
</head>
<body>

     <form action="process.asp" method="post" name="frmGuestbook">
	<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td> Name: </td>
		<td> <input type="text" name="name" value="" /> </td>
	</tr><tr>
		<td> Email Address: </td>
		<td> <input type="text" name="email" value="" /> </td>
	</tr><tr>
		<td> Message: </td>
		<td> <textarea name="message" colspan="50" rowspan="10"></textarea> </td>
	</tr><tr>
		<td> </td>
		<td> <iput type="submit" value="Submit Entry" /> </td>
	</tr>
	</table>
     </form>

</body>
</html>

Adding to the Database:
Now we have the HTML form we need the 'process.asp' page to process the messages into our database.

First of all we need to connect to the database, for this you need to follow our tutorial on Connecting To A Database.

After we have connected to our database we can proceed to insert the new message to our database. The following example shows us how we add data to a database.

<%
    Dim rsCommon
    Dim strSQL

    Set rsCommon = Server.CreateObject("ADODB.Recordset")

    strSQL = "SELECT * FROM tblGuestbook;"
    rsCommon.Open strSQL, adoCon, 3, 3
      rsCommon.AddNew
	rsCommon("Name")	= Request.Form("name")
	rsCommon("Email")	= Request.Form("email")
	rsCommon("Message")	= Request.Form("message")
      rsCommon.Update
    rsCommon.Close

    Set rsCommon = Nothing

%>

Remember to close the connection to the database

<%
    adoCon.Close
    set adoCon = Nothing
%>

Now that our data has been added the message to the database it would be a good idea to let the visitor know their message was added.

<html>
<head>
<title> Guestbook Entry Added </title>
</head>
<body>

	<h1>Message Added Successfully</h1>
	<p>Your Message was added successfully to our guestbook.</p>

</body>
</html>
Tutorial Writen By: Scotty32, 14th October 2006