To sum up, the operation on the database is nothing more
Display records, insert records, modify records, delete records, and query records.
In addition, it also involvesOutput Format and paging program displayedThen, we can combine the insert record into a simple news system, article system, message system, and registration and login system.
The following describes how to manage the database records.
First, which one to modify?
Modification is not a cage, but a specific modification. You can visually modify the specific row in the database table.
Therefore, at this time, the record set has a specific one. Of course, this is mainly determined by SQL statements.
For example, SQL = "select * from table where id = 1" indicates all records in the row where the extracted id number is 1, then, you only need to assign a new value to the field to be modified in the row and then upload it to the database.
The same statement SQL = "select * from table where id = 2" can be understood as well.
But as we are on the page, it is not so fixed, but there is a connection, or enter a form value ...... Jump to a special modification page, so that all the tasks are on the modification page, and its SQL statements should be adaptable.
For example, SQL = "select * from table where id =" & request. queyrstring ("id ")
Second, the corresponding value to be modified
Just like inserting a record, the field and value are matched.
Rs ("cn_name") = "cnbruce"
Rs ("cn_sex") = "male"
The corresponding value can also be a variable or function.
Finally, upload and update the database.
Perform rs. updata like insert. In fact, observe that inserting new records and update records only adds the declaration of rs. addnew.
1. showit. asp
This file is referenced in the previous example. It is mainly used for display. Now, a hyperlink is added to the modification page for a specific record.
<% For I = 1 to rs. PageSize 'uses the for next loop to read records of the current page in sequence If rs. EOF then Exit End if Response. write ("<a href = change. asp? Id = "& rs (" cn_id ") &"> modify </a> ") Response. write ("the title of the article is:" & rs ("cn_title ")) Response. write ("<br> author:" & rs ("cn_author ")) Response. write ("<br> when the article was added:" & rs ("cn_time ")) Response. write ("<br> content:" & rs ("cn_content ")) Response. write ("Rs. MoveNext Next %> |
Note:Response. write ("<a href = change. asp? Id = "& rs (" cn_id ") &"> modify </a> ")
The value of the parameter id is dynamic, and then we can see the capabilities of chang. asp.
2, change. asp
<! -- # Include file = "conn. asp" --> <% Id = request. querystring ("id ") %><% If request. form ("submit") = "change" then Whattitle = request. form ("title ") Whoauthor = request. form ("author ") Whatcontent = request. form ("content ") Id = request. form ("id ") Set rs = Server. CreateObject ("ADODB. Recordset ") SQL = "Select * from cnarticle where cn_id =" & id Rs. Open SQL, conn, 3, 2 Rs ("cn_title") = whattitle Rs ("cn_author") = whoauthor Rs ("cn_content") = whatcontent Rs. update Rs. close Set rs = Nothing Conn. close Set conn = Nothing Response. redirect ("showit. asp ") Response. end %> <% End if %> <% If id <> "" then Set rs = Server. CreateObject ("ADODB. Recordset ") SQL = "select * from cnarticle where cn_id =" & id Rs. Open SQL, conn, 1, 1 Whattitle = rs ("cn_title ") Whoauthor = rs ("cn_author ") Whatcontent = rs ("cn_content ") End if %> <Form action = "change. asp" method = "post"> Title: <input type = "text" name = "title" value = <% = whattitle %> <br> Author: <input type = "text" name = "author" value = <% = whoauthor %> <br> Content: <br> <Textarea name = "content" rows = "8" cols = "30"> <% = whatcontent %> </textarea> <br> <Input type = "submit" value = "change" name = "submit"> <Input type = "reset" value = "Reset"> <Input name = "id" type = "hidden" value = "<% = id %>"> </Form> |
Of course, all the procuratorates and security protection has not been done yet, and there are many bugs, so you can solve them slowly.
Another type of modification and update
<% If request. form ("submit") = "change" then Whattitle = request. form ("title ") Whoauthor = request. form ("author ") Whatcontent = request. form ("content ") Id = request. form ("id ")SQL = "update cnarticle set cn_title = '" & whattitle & "', cn_author = '" & whoauthor &"', cn_content = '"& whatcontent &" 'where cn_id = "& id Conn. Execute (SQL) Conn. close Set conn = Nothing Response. redirect ("showit. asp ") Response. end %> |