Learning purpose: Learn basic operations of database 1 (write records)
The basic operations of the database are nothing more than: querying records, writing records, deleting records, and modifying records. Today we will learn to write records first.
Create a form first:
<form name=form1 method=post action=example5.asp>
name <input type=text name=name><br>
tel <input type=text name=tel><br>
message <input type=text name=message value=><br>
<input type=submit name=Submit value=submit>
<input type=reset name=Submit2 value=Reset>
</form>
Submit the form to example5.asp, and the following is the code for example5.asp:
<%
set conn=server.createobject(adodb.connection)
conn.open driver={microsoft access driver (*.mdb)};dbq=&server.mappath(example3.mdb)
name=request.form(name)
tel=request.form(tel)
message=request.form(message)
exec=insert into guestbook(name,tel,message)values('+name+',+tel+,'+message+')
conn.execute exec
conn.close
set conn=nothing
response.write record added successfully!
%>
I won’t say the first two sentences here, and I won’t say the last three sentences. I said that the exec is the commands executed, and the records are added are quite complicated, so everyone should read them carefully. The name of the table is added after insert into, and the parentheses afterwards are fields that need to be added. The content of the field is the default value and can be omitted. Note that the variables here must correspond to the field names in ACCESS, otherwise an error will occur. The values are added to the transmitted variable. exec is a string, insert into guestbook(name,tel,message)values(' is the first paragraph, and double quotes cannot be embedded in ASP, so you can use ' instead of double quotes, put them in double quotes, and connect two variables Use + or & so ', another paragraph, and a name is inserted in the middle is the variable passed from the form, so you can add two '' outside this variable to indicate that it is a string, and the tel behind is a numeric variable, so There is no need to be surrounded outside, everyone analyzes this sentence slowly. If the data sent from the form is used instead of the variable name, the sentence is (assuming name=aaa,tel=111, message=bbb): insert into guestbook(name ,tel,message)values('aaa',111,'bbb').
The next conn.execute is to execute this exec command. Finally, don’t forget to close the open database and set the defined component to empty, so that the resource can be returned. I did not close the last reading for simplicity, so you can add it:
rs.close
set rs=nothing
conn.close
set conn=nothing
Remember, the order cannot be reversed!
You can go to the database to take a look, or use example4.asp to read to see if there are too many records?