Introduction to ASP Programming (20): ADO Component inserts data record _asp Foundation

Source: Internet
Author: User
Tags html form
A simple display record is in place, and what is needed is to insert the information into the database through ASP.

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
<textarea class="bk" id="temp21169" rows="12"><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></textarea>
[Ctrl + a All choose to copy tips: You can modify some of the code, and then click to 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")is to assign the values received in the previous page form to a variable that facilitates the invocation of the following program.

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

4,rs.addnewA simple and conspicuous statement: Create a new database Recordset row.

5,rs ("Cn_title") =whattitle, etc.The form value you are about to accept corresponds to the related field.

6,rs.updateOnly 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 viewIt needs to be explained that future ASP examples may be based on the first few sections of the content on the basis of completion.

8, releasing resourcesIt's the rules, 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, client detectionIn fact, through simple script detection, this is the beginning of the beginner ASP mentions the script, said, the following will repeat.

Strengthening of the addit.html
<textarea class="bk" id="temp12883" rows="12"><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 an article Content ") Document.form1.content.focus () return False}}--> </script> <form action=" addit.asp "method=" Pos T "Name=" Form1 "onsubmit=" Return Form1_onsubmit () "> Title:<input type=" text "name=" Title "><br> Author: <input type= "text" name= "author" ><br> content:<br> <textarea "Content" name= "8" rows= "30" ></textarea><br> <input type= "Submit" value= "Add" > <input type= "reset" value= "reset" > < /form></textarea>
[Ctrl + a All choose to copy tips: You can modify some of the code, and then click to Run]

2, server-side detectionOne might 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") =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
%>



As you can see, you are adding a similar judgment.


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


Very simply, if the message received is empty, the warning box is ejected, and the confirmation is returned to the previous page that addit.html. Of course Cn_author is not set to detect because the accepted value originally designed for this field is allowed to be empty.

OK, to sum up, the insertion record is mainly


Rs.addnew
RS ("Cn_title") =whattitle
...
Rs.update



Quite simply, declare the AddNew first, and then commit to insert the database.

This is a way to insert a database, and let's touch on the other one.

It should be noted that the Structured Query language, which is the SQL statement, can accomplish some of the more demanding database operations. Of course, the flow of information, such as extraction and insertion, is even easier. So the following method of inserting records is done directly using SQL syntax.

the modified addit.asp.


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

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

<%
sql = INSERT into cnarticle (cn_title,cn_author,cn_content) VALUES (' &whattitle& ', ' &whoauthor& ', ' "&whatcontent&") "
Conn. Execute (SQL)
%>

Article added successfully, <a href= "showit.asp" > Browse </a>

<%
Conn.close
Set conn=nothing
%>



In this program, you will find that as long as a line of SQL statements, do not need to establish Rescord recordset row, do not need to declare AddNew, do not execute update. Directly through the Conn. Execute (SQL) to complete.

Of course, by applying this method, You can also insert a record if the required field accepts a null value。 This and the introduction of the first Report


Microsoft JET Database Engine error ' 80040e21 '

Field ' xxx.xxx ' cannot be a 0-length string.


Has a great fault-tolerant nature.

Is the readability of the program or the simple and efficient way to take the choice of the king.
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.