Rookie 10 days of learning ASP Tutorial Fifth day: Write record

Source: Internet
Author: User
Tags insert variables query reset variable access
Tutorial | 10 Days of learning

Previous section: connecting and reading a database

Learning objectives: Learn the basic operation of the database-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: (Save the following article as 5.htm)

<form name= "Form1" method= "Post" action= "exa5.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 exa5.asp, and the following is the code for exa5.asp: (Save the following code as 5.asp)

<%
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Data/guestbook.mdb")
Name=request.form ("name")
Tel=request.form ("tel")
Message=request.form ("message")
Exec= "INSERT into Guest (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 guest (name,tel,message) VALUES ('" is the first paragraph, you cannot embed double quotes in an ASP, so you can use ' instead of double quotes, put in double quotes, connect two variables with + or & so " ', ' is also a section, the middle clip of a name is the form of the variable, so that you can add two "", is a string, the back of the Tel is a digital variable so do not need to surround 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 Guest (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.

The following statements are used to close the code for the database:

Rs.close
Set rs=nothing
Conn.close
Set conn=nothing

Remember, the order cannot be reversed! You can go to the database to see, or read with duqu.asp to see if there are more records ah? Here is a diagram of the database that I used to read from the previous section:

Next section: query Records



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.