In the front we introduced the file version of the guest book, now we might as well imagine: if the message file inside a lot of messages, and you are anxious to find a message in one of them, then open the TXT message file, to a paragraph of the message records, you dare to guarantee that you will not faint on the spot? And the most annoying thing about the file book is far more than that, more irritating: for example, you have found the message you are looking for, and is not to delete it (of course, because the message is mercilessly on your face smear the reason for it), then the rest of you want to praise the words of friends, You will not be able to leave a message because you are deleting the message. Look, how sad it is! However, you do not need to sigh, although the message file can not be used for many people, but changed to a database to manage the message records, when you do delete the message, the visitor's message can be added in. Since the database guest book can be so considerate to your system, we now without further ado, immediately to uncover her true colors.
As for how to build their own database version of the guest book, we still through a step-by-step approach to explain it. To write a database version of the guest book, we can roughly take the following three steps to complete.
Step One: Use Access to create a datasheet that holds the message. (Of course, if you want to make your database work well in high demand, customer-server databases for high-quality Web applications, and can withstand rigorous testing in performance and reliability, you'd better use server-level databases such as SQL Server, Oracle, and so on. For databases that are not server-level, I recommend using the Chinese version of access to build. The data tables that are created with access are the. mdb suffix name, such as: Book0.mdb. )
Access has a very friendly interface, the operation is also very simple, I believe that smart readers, even if never used access, as long as the hands to try, also will be able to quickly on its crossbow drive freely, so I will not be here to repeat (of course, do not rule out a few lazy ingredients).
Step two: Designing a Web page for a message form is also indispensable. (such as: guestbook0.htm).
Step three: And to write the processing of the message of the ASP program. (We can copy the code below and save it as a manage0a.asp file.) )
<!--#include file= "Adovbs.inc"-->
' Invoke the Adovbs.inc file, the so-called Inc file is meant to include file, Adovbs.inc is the IIS/PWS provided by the files, the definition of ADO related constants, using the Inc file can make our program, increase readability, Easier to develop and maintain.
<% Function Sqlstr (data)
Sqlstr = "'" & Replace (data, "'", "" ") &" "
End Function
' Here's a sqlstr function that changes one single quotation mark in the data to two single quotes, and it's preceded by a single quote. The reason for this function is that the data entered by the surfer must be combined with the INSERT INTO command in the form of a single quotation mark, but if the data entered by the user contains single quotes, the ' entered data ' will produce an error. It is therefore correct that the data entered must be SQLSTR (switching single quotes into two single quotes).
Name = Request ("Name")
Tel=request ("Tel")
email = Request ("email")
Subject = Request ("Subject")
Memo = Request ("Memo")
' Get data from the feedback form
If Name = "" or tel= "" or Email = "" or Subject = "" or Memo = "" Then Response.Write "input box cannot be blank!"
Response.End
End If
' Check the form's input boxes for information input, if left blank input box does not fill in the display of information: "Input box can not be blank!" and stop executing the following program.
Set conn = Server.CreateObject ("ADODB. Connection ")
' Use Server.CreateObject to get the object adodb.connection. Because in ASP, if you want to access data must first create a link to the database, so establishing a connection is an indispensable step to access the Web database, after executing the above statement, Conn is a connection object.
DBPath = Server.MapPath ("Book0.mdb")
' Gets the full path of the Book0.mdb by calling the MapPath method of the server object.
Conn. Open "Driver={microsoft Access driver (*.mdb)};d bq=" & DBPath
' Because we want to open an access (. mdb) database, we want to access the database through the ODBC driver {Microsoft access Driver (*.mdb)} in Access, and the DBP parameter is used to specify the database file that you want to open. Because it must be the full path name, we used the Server.MapPath function in the previous statement.
Name = Left (name, 40)
Tel = Left (tel,10)
email = left (email, 80)
Subject = Left (Subject, 127)
' Truncate input to the length of the data in each field, in order to avoid the error caused by the long data entered into a field, of course, if we set the data type of each field in the database Book0.mdb to "Memo", we don't need to worry about how long the data entered by the Internet users.
sql = "Insert into guestbook (name, phone, Email, subject, message) Values ("
sql = SQL & Sqlstr (Name) & ","
sql = SQL & Sqlstr (Tel) & ","
sql = SQL & Sqlstr (Email) & ","
sql = SQL & Sqlstr (Subject) & ","
sql = SQL & Sqlstr (Memo) & ")"
' Add a data record to the ' guestbook ' datasheet and then set each field (name, phone, Email, subject, message) to the corresponding data from the message form.
Conn. Execute SQL
' Execute the SQL directive.
%>