ASP Advanced Tutorial: Database version of the guest book

Source: Internet
Author: User
Tags include insert sql access web database
Tutorials | data | Database in the previous 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 to 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.) )


$#@60!--#include file= "Adovbs.inc"--$#@62;


call Adovbs.inc file, the so-called inc file is the meaning of include file, Adovbs.inc is IIS/PWS provided by the file, stored in the definition of ADO related constants, using the Inc file can make our program, increase readability, Easier to develop and maintain.


$#@60; % Function sqlstr (data)


sqlstr = "" & Replace (Data, "") & ""


End Function


Here you use the SQLSTR function, which is to change one single quotation mark in the data to two single quotes, and precede it with single quotes. This function is used because 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 data entered 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 message 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 is not filled out to display the information: "The input box can not be blank!" and stop executing the following program.


Set conn = Server.CreateObject ("ADODB. Connection ")


use Server.CreateObject to obtain 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 invoking 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 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


executes the SQL instructions.


%$#@62;





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.