How to insert a record (add information) to a database using the insert into statement in asp

Source: Internet
Author: User

1. 'create register. asp

Copy codeThe Code is as follows: <% @ language = vbscript %>
<Html>
<Head>
<Title> registration page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Style type = "text/css">
Td img {display: block;}. STYLE2 {font-size: 12px}
. STYLE3 {
Color: # FF0000;
Font-weight: bold;
Font-size: 16px;
}
</Style>
</Head>
<Body bgcolor = "# ffffff">
<Table width = "531" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<! -- DWLayoutTable -->
<! -- Fwtable fwsrc = "unnamed" fwbase = "register.jpg" fwstyle = "Dreamweaver" fwdocid = "1502317357" fwnested = "0" -->
<Tr>
<Td width = "58" rowspan = "5" valign = "top" bgcolor = "# D8FBB5"> </td>
<Td width = "209" height = "75" valign = "top"> </td>
<Td width = "204" valign = "top"> </td>
<Td width = "60" rowspan = "5" valign = "top" bgcolor = "# D8FBB5"> </td>
</Tr>
<Tr>
<Td height = "50" colspan = "2" valign = "top"> </td>
</Tr>
<Tr>
<Td height = "16"> </td>
<Td> </td>
</Tr>
<Tr>
<Td height = "42" colspan = "2" valign = "top"> <form id = "form1" name = "form1" method = "post" action = "result2.asp">
<Table width = "100%" border = "1">
<Tr>
<Td colspan = "3"> <span class = "STYLE2"> enter your registration information below, red <span class = "STYLE3"> * </span> indicates a required option. </span> </td>
</Tr>
<Tr>
<Td align = "center" valign = "middle"> User name: </td>
<Td> <label>
<Input type = "text" name = "u_user" id = "u_user"/>
</Label> </td>
<Td> <span class = "STYLE2"> <span class = "STYLE3"> * </span> </td>
</Tr>
<Tr>
<Td align = "center" valign = "middle"> password: </td>
<Td> <label>
<Input type = "text" name = "u_pass" id = "u_pass"/>
</Label> </td>
<Td> <span class = "STYLE2"> <span class = "STYLE3"> * </span> </td>
</Tr>
<Tr>
<Td align = "center" valign = "middle"> name: </td>
<Td> <label>
<Input type = "text" name = "I _name" id = "I _name"/>
</Label> </td>
<Td> <span class = "STYLE2"> <span class = "STYLE3"> * </span> </td>
</Tr>
<Tr>
<Td align = "center" valign = "middle"> Gender: </td>
<Td> <label>
<Select name = "I _sex" size = "1" id = "I _sex">
<Option value = "1"> male </option>
<Option value = "2"> female </option>
</Select>
</Label> </td>
<Td> <span class = "STYLE2"> <span class = "STYLE3"> * </span> </td>
</Tr>
<Tr>
<Td align = "center" valign = "middle"> Age: </td>
<Td> <label>
<Input type = "text" name = "I _age" id = "I _age"/>
</Label> </td>
<Td> <span class = "STYLE2"> <span class = "STYLE3"> * </span> </td>
</Tr>
<Tr>
<Td colspan = "3" align = "center" valign = "middle"> <label>
<Input type = "submit" name = "button" id = "button" value = "submit Registration materials"/>

<Input type = "reset" name = "button2" id = "button2" value = "reset form"/>
</Label> </td>
</Tr>
</Table>
</Form> </td>
</Tr>
<Tr>
<Td colspan = "2" valign = "bottom" bgcolor = "# D8FBB5"> <! -- DWLayoutEmptyCell --> </td>
</Tr>
</Table>
</Body>
</Html>

2. Create a connection and open the database subroutine con1.aspCopy codeThe Code is as follows: <%
'Method for connecting strings to the database
Set conn = server. createobject ("adodb. connection ")
Conn. Open "DRIVER = Driver do Microsoft Access (*. mdb); uid = admin; pwd =; DBQ =" & server. MapPath ("regdb1.mdb ")
%>

3. Create the ACCESS database db1.mdb and create the data table user. The fields are u_uid (automatic), u_user (user name: text type), and u_pass (password: text type)
4. Create a form to receive data sent from register. asp and insert it to the registration successful page of the user in the data table. The result2.asp code is as follows:Copy codeThe Code is as follows: <% @ language = vbscript %>
<! -- # Include file = "con1.asp" -->
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Registration successful </title> <br>
</Head>
<Body>
<%
'Create recordset object Method
Set rst = server. createobject ("adodb. recordset ")
Set rst1=conn.exe cute ("insert into user (u_user, u_pass) values ('" & request. form ("u_user") & "','" & request. form ("u_pass ")&"')")
%>
<Table width = "300" border = "0" align = "center">
<Tr>
<Td align = "center"> </td>
</Tr>
</Table>
</Body>
</Html>

5. Press F12 to preview register. asp, enter data, and click Submit.


The user who opens the data table will see:

The operation is successful. Because the operation has been submitted twice, we can see that two new records are inserted in the table, but there are repeated records in the table. The following code is added in result2.asp to solve the problem:Copy codeThe Code is as follows: <% @ language = vbscript %>
<! -- # Include file = "con1.asp" -->

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Registration successful </title>
<Style type = "text/css">
<! --
. STYLE1 {font-size: 24px}
-->
</Style>
<Br> <%
'Create recordset object Method
Set rst = server. createobject ("adodb. recordset ")
Rst. open "select * from user where u_user = '" & request. form ("u_user") & "'", conn, 1, 1
If rst. recordcount> 0 then
Response. write "username:" & request. form ("u_user") & "occupied, please click back to register"
%>
<A href = "register. asp" class = "STYLE1"> return to re-register </a>
<%
Response. End
Else
Set rst1=conn.exe cute ("insert into user (u_user, u_pass) values ('" & request. form ("u_user") & "','" & request. form ("u_pass ")&"')")
End if
%>
<Table width = "300" border = "0" align = "center">
<Tr>
<Td align = "center"> </td>
</Tr>
<Tr>
<Td align = "center"> <a href = "register. asp" class = "STYLE1"> return </a> </td>
</Tr>
</Table>
</Body>
</Html>

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.