Using ASP to create personalized forum (middle)

Source: Internet
Author: User
Tags array sql query variable
Second, the construction

1. Building the main form

First to give a form to allow users to enter the registration message, these are HTML content, and put aside, we take a closer look at the specific implementation of the registered ASP script.


(1) Change the single quotation mark in the data to two single quotes and enclose the single quotation mark before and after

Function sqlstr (data)

Sqlstr = "'" & Replace (data, "'", "" ") &" "

End Function

Note: This is a custom function that converts single quotes (') in user input to two single quotes ('). In ASP, the string is surrounded by double quotes, so the above "'" means a string with only one single quote. The reason to change one single quotation mark into two single quotes is that it is used to represent variables in a SQL statement enclosed in single quotes. To avoid confusion, the single quotation marks in the string are represented by two single quotes. All user input is embedded as a variable in the SQL statement, so this function is essential.

(2) Storage preparation

Id=request ("id")

Password=request ("password")

Nickname=request ("nickname")

Email=request ("email")

Sex=request ("Sex")

Note: It is not necessary to save the content from the user input form in a variable, but it is easier to read and write.

If Request ("name") = "" Then Name= "Else name=request (" name ")

If Request ("phone") = "" Then phone= "" Else phone=request ("phone")

Because these content is not mandatory, in order to prevent users from not entering any content, and caused errors in database operations, you must not fill in the field with a space to replace.

(3) Establish a connection

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

Conn. Open "Driver={microsoft accessdriver (*.mdb)};d bq=" & Server.MapPath ("Bbssystem.mdb")

Note: This section is to establish a database connection, the name of the database is Bbssystem.mdb, the only thing to note in this paragraph is the application of the Server.MapPath function. In general, where the specific directory is involved, do not use the directory name directly, instead of using the Server.MapPath function instead. Make good use of Server.MapPath and Request.ServerVariables () and other functions, you can have a better Web application portability.

Set cmd = Server.CreateObject ("Adodb.command")

(4) query whether the author already exists

Set cmd. ActiveConnection = conn

Cmd.commandtext = "query Author"

ReDim param (0) ' declares an array of arguments

Param (0) = CStr (ID) ' Cint not to be ignored

Set rs = cmd. Execute (, param)

Note: This section is used to execute the stored procedure. There are many ways to execute queries in ADO, but you can only use command objects for stored procedures. First, a command object called CMD is established, then the Conn Connection object is set to the ActiveConnection attribute of the Cmd object, the query name "query Author" is set to the CommandText attribute, and then the query parameter is assigned a value. We declare a parameter array param (0), because there is only one argument in the query author query, so the array has only one component. In general, there are several parameters in the query, it is necessary to declare a corresponding number of components of the parameter array. and the order in which the parameters appear is corresponding to the order of the components in the array. In the process of using a parameter query, it is particularly noteworthy that the type of the parameter should be strictly matched, or this will be an error, so the above CStr () type conversion function is indispensable.

If not (rs.eof or RS.BOF) then

Response.Write "Error, you entered the ID number has been occupied, please change another try again!" "

Else

sql = "Insertsintos author table (ID, nickname, Email, password, name, school, department, Sex, telephone) Values ("

sql = SQL & Sqlstr (ID) & ","

sql = SQL & Sqlstr (nickname) & ","

sql = SQL & sqlstr (email) & ","

sql = SQL & sqlstr (password) & ","

sql = SQL & SQLSTR (name) & ",&", "

sql = SQL & sqlstr (Sex) & ","

sql = SQL & SQLSTR (phone) & ")"

Conn. Execute SQL

Inserts the data into the database using a SQL INSERT statement. In fact, this query can be made into a stored procedure in the database, I stole a bit lazy:-) but the contrast can also see the benefits of stored procedures, the implementation of the query write it is too troublesome.

2, the construction article display module

As I said before, a specific article is divided between the Kanban and the subject. Therefore, it is possible to get a list of articles under a specific topic by displaying the articles through both the Kanban list and the main list.

(1) Board list display page:



<title> Kanban List </title>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">


<%

(2) Open the connection, display the Kanban list

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

Conn. Open "Driver={microsoft accessdriver (*.mdb)};d bq=" & Server.MapPath ("Bbssystem.mdb")

sql = "SELECT * FROM Kanban list"

Set Rs=conn.execute (SQL)

%>

Note: A simple SQL query that passes all Kanban-related messages back to the recordset Rs. The following task is to display the record and remember the content, and set up a connection to display the Kanban theme on the corresponding Kanban name.

<body bgcolor= "#FFFFFF" >

&LT;H2 align= "center" > Kanban list
<table width= "60%" border= "0" cellspacing= "0" cellpadding= "0" align= "center" >

<tr bgcolor= "#FFFFCC" >

&LT;TD height= "width=" 25% > Kanban name </td>

&LT;TD height= "width=" "21%" > Board main </td>

&LT;TD height= "width=" "23%" > Theme number </td>

&LT;TD height= "width=" 31% > Board main landing </td>

</tr>

Note: Here is a section that shows the title of each column in the table, where I do not use a uniform function to display the contents of Rs records, because it can be more control of the appearance and style of the table, although it is a bit cumbersome to use, but more flexible.

<%

Todo

BOARDID=RS ("id")

Boardname=rs ("name")

Boardmanager=rs ("board Master")

Response.Write "<tr><td><a href=qboard.asp?boardid=" & boardid& "&boardname=" & boardname& ">" & boardname & "</a></td>"

Note: This line is the focus, and when clicked on the name of each board, you can connect to the page that displays the Kanban theme. The program code looks a bit cumbersome, I break it down for you, and you'll understand. After clicking, the browser requests a qboard.asp page with a parameter boardid representing the ID number of the Kanban to display, and a question mark (?) between the request page and the parameters. Separated Boardid is the previously set variable, which contains the ID number of the corresponding kanban. This connection is also accompanied by another parameter boardname, which is used to pass the Kanban name to the qboard.asp page. Use "&" between multiple parameters to separate. This parameter is not required and is passed in order to avoid querying the Kanban name again with Boardid in the qborad.asp. In general, the ability to use less database operations should be as small as possible, which can improve the performance of ASP pages. Because the Response.Write statement uses a string as a parameter, the above string and variable use the "&" join character. The final ASP page explain the result should be like this

<td><a href=qboard.asp?boardid=1&boardname= System Board > System Board </a><td>.

Response.Write "<td><a href=qauthor.asp?author=" &boardmanager & > "&boardmanager &" < /a></td> "

Response.Write "<td>" &rs ("Subject number") & "</td>"

Response.Write "<td><a href=managerlogin.asp?boardid=" &boardid & "> Board handling </a></td> </tr> "

Note: In this table, the contents of a Kanban topic can be displayed in addition to the connection



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.