Tutorial/asp 10 days to learn the fifth day of ASP

Source: Internet
Author: User
Tags add insert variables reset string variable access

Learning objectives: Learn the basic operation of the database 1 (Write records)
The basic operation of the database is: Query records, write records, delete records, modify records. Today we 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= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "reset" >
</form>

The form is submitted to example5.asp, and the following is example5.asp code:
<%
Set Conn=server.createobject (" Adodb.connection ")
Conn.Open" Driver={microsoft Access Driver (*.mdb)};d bq= "&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 success!"
%>

here in front of two I do not say, the back of three I do not say, said in front of exec is the implementation of the command, add records of the more complex, we should look carefully. Insert into followed by the name of the table, followed by the parentheses inside the need to add the field, do not add or the contents of the field is the default value can be omitted. Notice that the variables here must correspond to the field names in Access, or else they will go wrong. Values are followed by the transmitted variables. exec is a string, "insert into Guestbook (name,tel,message) VALUES (\" "is the first paragraph, in the ASP can not be embedded in double quotes, so you may use \ ' instead of double quotes, placed in double quotes, connect two variables with + or & So "\," another section, the middle clip of a name is the form of the variable, so that you can add two in the variable "\", said to be a string, the back of the Tel is a digital variable so do not need to surround the outside of the ' \ ', we slowly analyze this sentence, If you use the data from the form to replace the variable name, this sentence is (suppose 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, and don't forget to shut down the open database and set the defined component to null so that the resource can be returned. Last read in order to be simple, I did not close, everyone can add up:
Rs.close
Set rs=nothing
Conn.close
Set conn=nothing
Remember, the order cannot be reversed!



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.