Introduction to ASP Programming (20): Insert data record of ADO component

Source: Internet
Author: User
Tags end html form include insert sql reset return trim
ado| Programming | insert | Data |ado simple display record already mastered, now need is through ASP to insert information content into the database.

One, own database Cnbruce.mdb

The purpose of this database is to be inserted into the data, you can directly use the Cnbruce.mdb file already established in the previous section, of course, the connection to open the database file conn.asp also successfully cited.

Second, the establishment input inserts the information the page platform addit.html

The main function of this page is to display some text input boxes to provide input information to the content submission database.

1,addit.html
<form action= "addit.asp" method= "POST" >
Title:<input type= "text" name= "title" ><br>
Author:<input type= "text" name= "Author" ><br>
Content:<br>
<textarea name= "Content" rows= "8" cols= "></textarea><br>"
<input type= "Submit" value= "ADD" >
<input type= "reset" value= "reset" >
</form>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

This page is actually very simple, that is, through the Submit button to the form of the information content submitted to the Addit.asp page to deal with.

Third, the establishment processing accepts the data and inserts the database table the page program addit.asp

2,addit.asp


<!--#include file= "conn.asp"-->

<%
Whattitle=request.form ("title")
Whoauthor=request.form ("author")
Whatcontent=request.form ("content")
%>

<%
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * FROM Cnarticle"
Rs. Open sql,conn,3,2
%>

<%
Rs.addnew
RS ("Cn_title") =whattitle
RS ("Cn_author") =whoauthor
RS ("Cn_content") =whatcontent
Rs.update
%>
Article added successfully, <a href= "showit.asp" > Browse </a>
<%
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
%>




Well, then it's a concrete analysis.

1,<!--#include file= "conn.asp"--> Needless to say, any connection to the database should be applied to the link file.

2,whattitle=request.form ("title"), and so on, is to assign the values accepted in the previous page form to a variable to facilitate the invocation of the following program.

3,rs. Open sql,conn,3,2 Note the difference between parameters and the parameters used when displaying the database.

4,rs.addnew is simple and conspicuous. A declaration: Create a new database Recordset row.

The 5,rs ("Cn_title") =whattitle, such as the pending form values, correspond to the related fields.

6,rs.update is only the value and database field correspondence, after the corresponding value upload to submit to the database table.

7, after inserting can jump to showit.asp view need to explain, the future ASP examples may be based on the first few sections of the content on the basis of completion.

8, releasing resources is the rule, don't forget.

Well, now. inserting and displaying database records is a complete piece of cake for you. The general frame is completed, the following is the specific refinement.

This includes: The client's form detection, to prevent the omission of user information input, of course, it is best to add server-side form detection, due to the client for some reasons (such as self-designed to submit non-test pages) based on security considerations, all or according to the server-side accepted information. Then continue to refine the application below.

1, the client-side detection is actually through a simple script detection, which in the beginning of the beginner ASP mentions the script, said, the following will repeat.

Strengthening of the addit.html
<script laguage= "JavaScript" >
<!--
function Form1_onsubmit ()
{
if (document.form1.title.value== "")
{
Alert ("Please enter article title")
Document.form1.title.focus ()
return False
}
else if (document.form1.content.value== "")
{
Alert ("Please enter article content")
Document.form1.content.focus ()
return False
}
}
-->
</script>

<form action= "addit.asp" method= "Post" Name= "Form1" onsubmit= "Return Form1_onsubmit ()" >
Title:<input type= "text" name= "title" ><br>
Author:<input type= "text" name= "Author" ><br>
Content:<br>
<textarea name= "Content" rows= "8" cols= "></textarea><br>"
<input type= "Submit" value= "ADD" >
<input type= "reset" value= "reset" >
</form>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

2, server-side detection Some people may ask, since there is a client detection, there are multiple server-side detection? The reason is simple, like the HTML form submission page, design a page to be removed from the form detection. When you click Submit and there is no information, reaching the server side without any defenses will certainly result in data insertion failure.

Strengthening of the addit.asp


<!--#include file= "conn.asp"-->

<%
Whattitle=trim (Request.Form ("title"))
Whoauthor=trim (Request.Form ("author"))
Whatcontent=trim (Request.Form ("content"))
%>

<%if whattitle= "" then%>
<script language=vbs>
Alert ("Please enter article title")
History.go (-1)
</script>
<%end if%>
<%if whatcontent= "" then%>
<script language=vbs>
Alert ("Please enter article title")
History.go (-1)
</script>
<%end if%>

<%
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * FROM Cnarticle"
Rs. Open sql,conn,3,2
%>

<%
Rs.addnew
RS ("Cn_title") =whattitle
RS ("Cn_author&q



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.