The eighth day of learning ASP in ten days
Author:Eve Cole
Update Time:2009-06-20 17:22:00
Learning purpose: Learn basic database operations 4 (modify records)
Let’s look at the code first:
<%
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("test.mdb")//This is not the previous database, there are only two fields aa and bb in it
exec="select * from test where id="&request.querystring("id")
set rs=server.createobject("adodb.recordset")
rs.open exec,conn
%>
<form name="form1" method="post" action="modifysave.asp">
<table width="748" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>aa</td>
<td>bb</td>
</tr>
<tr>
<td>
<input type="text" name="aa" value="<%=rs("aa")%>">
</td>
<td>
<input type="text" name="bb" value="<%=rs("bb")%>">
<input type="submit" name="Submit" value="Submit">
<input type="hidden" name="id" value="<%=request.querystring("id")%>">
</td>
</tr>
</table>
</form>
<%
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
Everyone should analyze that there is nothing wrong with this code by now. The function of this code is to accept the ID of the previous page and then display this record. The text box is both the input place and the display place. If you need to modify it, press submit after modification; if You can just press the submit button without making any changes. There is another thing here that has not been mentioned before, and that is the hidden form element: the hidden element. The value inside does not need to be entered by the user. It will be submitted along with the form and used to pass variables. Below is the code for modifysave.asp:
<%
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("test.mdb")
exec="select * from test where id="&request.form("id")
set rs=server.createobject("adodb.recordset")
rs.open exec,conn,1,3
rs("aa")=request.form("aa")
rs("bb")=request.form("bb")
rs.update
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
Here, the parameters following rs.open exec,conn,1,3 are 1,3. As I mentioned before, 1,3 must be used to modify the record. In fact, it is easy to understand when modifying records. The record set is rs. rs("aa") is what currently records the aa field. Make it equal to the new data request.form("aa"). Of course, it will be modified, but don't change it at the end. Forgot to save, that's rs.update!
Speaking of which, record search, reading, modification, and insertion have all been mentioned. By using these most basic things, you can make complex things. Large databases outside: news systems, guest books, etc. just have a few more fields. The code in today's example is combined with the previous database. Please go back and debug and analyze it after you download it. (The example72.asp in the rar is still for everyone to query the record ID and check the modified records)