The simple display record has been mastered. Now, you need to insert the information content to the database through ASP.
1. Have database cnbruce. mdb
This database is used to insert data. You can directly use the cnbruce. mdb file that has been created in the previous section. Of course, the connection to the database file conn. asp will be referenced smoothly.
2. Create the page platform addit.html for inputting information
The main function of this page is to display some text input boxes to provide input information for submission to the database.
1. addit.html
<Form action = "addit. asp "method =" post "> <br/> Title: <input type =" text "name =" title "> <br/> Author: <input type = "text" name = "author"> <br/> Content: <br> <br/> <textarea name = "content" rows = "8" cols = "30"> </textarea> <br/> <input type = "submit" value = "Add"> <br/> <input type = "reset" value = "Reset"> <br/> </form> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
This page is actually very simple. You can use the submit button to submit the form information to the addit. asp page for processing.
3. Create a page program addit. asp to process and accept data and insert the database table
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 %> The article is successfully added. <a href = "showit. asp"> browse </a> <% Rs. close Set rs = Nothing Conn. close Set conn = Nothing %> |
Well, let's analyze it in detail.
1, <! -- # Include file = "conn. asp" -->Needless to say, the connection file must be applied to all files associated with the database.
2, whattitle = request. form ("title") and so onIt is to assign the value accepted in the form on the previous page to a variable to facilitate the call of the following program.
3, rs. Open SQL, conn, 3, 2Note that the parameters are different from those used to display the database.
4, rs. addnewA simple and conspicuous statement: creates a database record set row.
5, rs ("cn_title") = whattitle and so onThe form values to be accepted correspond to related fields.
6. rs. updateOnly the value corresponds to the database field, and then the corresponding value is uploaded and submitted to the database table.
7. After insertion, you can jump to showit. asp to viewIt should be noted that the ASP examples in the future are based on the content of the previous sections.
8. release resourcesIt's a rule. Don't forget it.
So, now. Inserting and displaying database records is a piece of cake for you. After the general framework is set up, the following details are provided.
These include: Form detection on the client to prevent omission of user information input; of course, it is best to add form detection on the server side, because of some reasons on the client (such as self-design to submit non-detection pages) based on security considerations, everything is subject to the information accepted by the server. Next we will continue to refine the application.
1. Client DetectionIn fact, it is through simple script detection. As mentioned when ASP is a beginner, we will repeat it below.
Enhanced addit.html
<Script laguage = "javascript"> <br/> <! -- <Br/> function form1_onsubmit () <br/>{< br/> if (document. form1.title. value = "") <br/>{< br/> alert ("Enter the document title") <br/> document. form1.title. focus () <br/> return false <br/>}< br/> else if (document. form1.content. value = "") <br/>{< br/> alert ("Enter the document content") <br/> document. form1.content. focus () <br/> return false <br/>}< br/> --> <br/> </script> </p> <form action = "addit. asp "method =" post "name =" form1 "onsubmit =" return form1_onsubmit () "> <br/> Title: <input type = "text" name = "title"> <br/> Author: <input type = "text" name = "author"> <br/> Content: <br> <br/> <textarea name = "content" rows = "8" cols = "30"> </textarea> <br/> <input type = "submit" value = "Add"> <br/> <input type = "reset" value = "Reset"> <br/> </form> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
2. Server DetectionSome people may ask, since there is a client detection, how many server detection? The reason is very simple. For example, you can design a page to remove the form detection, just like the HTML form submission page. When you click Submit without any information, data insertion will certainly fail if you reach the server without any defense.
Enhanced 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 ("Enter the article title ") History. go (-1) </Script> <% End if %> <% If whatcontent = "" then %> <Script language = vbs> Alert ("Enter the 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 %> The article is successfully added. <a href = "showit. asp"> browse </a> <% Rs. close Set rs = Nothing Conn. close Set conn = Nothing %> |
We can see that the following similar judgment is added.
<% If whattitle = "" then %> <Script language = vbs> Alert ("Enter the article title ") History. go (-1) </Script> <% End if %> |
If the received information is empty, a warning box is displayed, and the confirmation is sent back to the page addit.html. Of course, cn_author does not set the detection, because the Acceptance value of this field was originally designed to be null.
OK. To sum up, the insert record is mainly
Rs. addnew Rs ("cn_title") = whattitle ... Rs. update |
It's easy to declare addnew first, then one-to-one correspondence, and then commit to insert the database.
This is a way to insert data into the database. Next, let's take a look at another method.
Note that structured query languages, that is, SQL statements, can perform some demanding database operations. Of course, it is even more difficult to extract and insert information streams. Therefore, the following insert record method is to directly use SQL syntax.
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) %> The article is successfully added. <a href = "showit. asp"> browse </a> <% Conn. close Set conn = Nothing %> |
In this program, you will find that as long as one SQL statement, you do not need to create a Rescord set row, do not need to declare addnew, do not execute update. Directly through conn. Execute (SQL.
Of course, this method is used,If the accept value of the required field is null, the record can also be inserted.. This is the first report
| Microsoft JET Database Engine error '80040e21' The field 'xxx. XXX' cannot be a zero-length string. |
High Fault tolerance.
Whether the program is readable or the method is simple and efficient.