Today we will write a message board program with management functions. The so-called management function actually means that there is a selection box in front of each message. Click on the message to be deleted, or click "Delete all messages" to complete the batch deletion. Through this example, we can have a preliminary understanding of the basic operations of the database in ASP.
1. Establish a database (ACCESS2000 Chinese version)
myid text type 20 bytes This is the only sign of speech
myname text type 10 bytes This is the name of the person who left the message
nowtime date/time type The message time is recorded here
mytitle text type 30 bytes This is the title of the message
mybody remarks message content
The myid field is the key, it is the only sign of deletion.
2. Management program
manage.asp code
'Define variables first
dim listrs,conn,cmd
'The number of cycles of i can be modified according to the actual number of messages
for i=1 to 100
if request("delit"&i)<>empty then
'The above statement is the key part. Check whether the i-th message is selected. If it is selected, perform the following deletion operation.
Set conn = Server.CreateObject("ADODB.Connection")
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("guestbook.mdb")&";"
'The mappath function uses a parameter guestbook.mdb, indicating the database to be connected. Pay attention to the path problem.
conn.Open sConnection
'Reconnect the data table
set cmd=Server.Createobject("ADODB.Command")
set cmd.activeconnection=conn
cmd.commandtext="delete from guest where myid='"&request("delit"&i)&"'"
'The guest above is the table name of the data table, and myid is the field name
cmd.execute
set conn=Nothing
set cmd=Nothing
'Execute SQL instructions and release objects
end if
next
'Output the form, note that the action attribute points to manage.asp
response.write ""
'Connect to database
set conn=Server.CreateObject("ADODB.Connection")
set listrs=server.createobject("ADODB.Recordset")
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("guestbook.mdb")&";"
conn.Open sConnection
listrs.open "select * from guest",conn,2,2
'The guest table of the database is opened above
i=1
'Variable i starts counting
while not listrs.eof
response.write "From:"&listrs("myname")&"----"&listrs("mytitle")&"Time:"&listrs("nowtime")&"
"&listrs("mybody")&"
"
i=i+1
'The above two steps are the key points. While outputting each message, output a checkbox. The function of this box is to return different values when checked or unchecked.
listrs.movenext
wend
set listrs=Nothing
'Output submit button and form end flag
response.write
3. Code analysis
The code is not easy to understand, so let me explain:
1. When the program is executed for the first time, if request("delit"&i)<>empty then always returns a false value because the check box is not selected, and then all messages are output.
2. After selecting the message to be deleted, press the "Delete button" and re-execute.
3. After deleting all selected messages, output the message board.