The fifth day of the 10 Day Society ASP

Source: Internet
Author: User
Learning Purpose: Learn DatabaseBasic Operation 1 (write record)
DatabaseThe basic operation 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 the code for example5.asp:
<%
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 could use ' instead of double quotes, put in double quotes, connect two variables with + or & So "', ' another paragraph, the middle clip of a name is the form of the variable, so that you can add two" ", which 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 Guestbook (name,tel,message) VALUES (' AAA ', , ' BBB ').
The next conn.execute is to execute this EXEC command, and in the end don't forget to open the DatabaseClose, put the definition of ComponentSet 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!
Everyone can come to DatabaseTake a look inside, or read with example4.asp to see if there is more records?


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.