Tutorial | 10 Days of learning
Previous section: writing a record
Learning Purpose: Learn the basic operation of database--query record
On the fourth day we use one of the following programs:
<%
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Data/guestbook.mdb")
Exec= "SELECT * FROM Guest"
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open exec,conn,1,1
%>
We're looking at all the records, but we're not going to be able to get all the records when we want to modify or delete the records, all of us have to learn to retrieve the right records. First look at a statement:
A= "AAA"
b=1111110
Exec= "SELECT * from guestbook where name= '" +a+ "' and Tel= ' +b
Where the following is the condition, with and, or, I think everyone knows the meaning of =,<=,>=,<,>. The search name is "AAA" and the phone is a record of "1111110". The other thing is if you're searching for a field that contains a string, you can write this: where InStr (Name,a) is the person who searches for the string of a (AAA) in name.
My a,b here, is a constant, you can let A,b is the form submitted to the variable, so you can do a search.
Let's take a look at this code and understand this: (Save the following code as a 6.htm file)
<form name= "Form1" method= "Post" action= "exam6.asp" >
Search:<br>
Name =
<input type= "text" name= "name" >
and tel=
<input type= "Text" name= "Tel" >
<br>
<input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "reset" >
</form>
exam6.asp (Save the following code as exam6.asp)
<%
Name=request.form ("name")
Tel=request.form ("tel")
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};d bq=" &server.mappath ("Data/guestbook.mdb")
Exec= "SELECT * from guest where name= '" "+name+" ' and Tel= ' +tel
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open exec,conn,1,1
%>
<title> Untitled Document </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body bgcolor= "#FFFFFF" text= "#000000" >
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >
<%
Do as not rs.eof
%><tr>
<td><%=rs ("name")%></td>
<td><%=rs ("tel")%></td>
<td><%=rs ("message")%></td>
<td><%=rs ("Time")%></td>
</tr>
<%
Rs.movenext
Loop
%>
</table>
</body>
First enter http://localhost/6.htm in the browser as shown in the following image:
Enter the name and phone aaa,1111110 that are already in the database, and click Submit, as shown in the following figure:
Next section: deleting a record