method for inserting records (adding information) into a database using INSERT INTO statements in ASP application tips

Source: Internet
Author: User

I. ' Establishment of REGISTER.ASP

Copy Code code as follows:

<%@ Language=vbscript%>
<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>
<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>
&LT;TD width= "rowspan=" "5" valign= "Top" bgcolor= "#D8FBB5" ></td>
&LT;TD width= "209" height= "a" valign= "top" ></td>
&LT;TD width= "204" valign= "top" ></td>
&LT;TD width= "rowspan=" "5" valign= "Top" bgcolor= "#D8FBB5" ></td>
</tr>
<tr>
&LT;TD height= colspan= "2" valign= "Top" ></td>
</tr>
<tr>
&LT;TD height= "></td>"
<td></td>
</tr>
<tr>
&LT;TD height= "colspan=" "2" valign= "Top" ><form id= "Form1" Name= "Form1" method= "Post" action= "result2.asp" >
<table width= "100%" border= "1" >
<tr>
&LT;TD colspan= "3" ><span class= "STYLE2" > Please fill in your registration information below, red <span class= "STYLE3" >*</span> The options indicated are required </span></td>
</tr>
<tr>
&LT;TD align= "center" valign= "Middle" > Username:</td>
<td><label>
<input type= "text" name= "U_user" id= "U_user"/>
</label></td>
<td><span class= "STYLE2" ><span class= "STYLE3" >*</span></span></td>
</tr>
<tr>
&LT;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></span></td>
</tr>
<tr>
&LT;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></span></td>
</tr>
<tr>
&LT;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" > Women </option>
</select>
</label></td>
<td><span class= "STYLE2" ><span class= "STYLE3" >*</span></span></td>
</tr>
<tr>
&LT;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></span></td>
</tr>
<tr>
&LT;TD colspan= "3" align= "center" valign= "Middle" ><label>
<input type= "Submit" name= "button" id= "button" value= "Submission of registration information"/>

<input type= "reset" name= "Button2" id= "Button2" value= "Reset form"/>
</label></td>
</tr>
</table>
</form></td>
</tr>
<tr>
&LT;TD colspan= "2" valign= "Bottom" bgcolor= "#D8FBB5" ><!--dwlayoutemptycell--> </td>
</tr>
</table>
</body>

Second, establish a connection and open the database subroutine con1.asp
Copy Code code as follows:

<%
' Method of string connection to database
Set Conn=server.createobject ("Adodb.connection")
Conn. Open driver=driver do Microsoft Access (*.mdb); uid=admin;pwd=;D bq= "&server. MapPath ("Regdb1.mdb")
%>

Third, the establishment of Access database Db1.mdb, in which the data table user, the fields are: U_uid (Automatic), U_user (user name: Text type), U_pass (password: text type)
Set up the form data sent from the receiving register.asp and insert it into the data table user to register the successful display page: The result2.asp code is as follows:
Copy Code code as follows:

<%@ Language=vbscript%>
<!--#include file= "con1.asp"-->
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Registration Success </title><br>
<body>
<%
' Create a Recordset object method
Set Rst=server.createobject ("Adodb.recordset")
Set Rst1=conn.execute ("INSERT into User (U_user,u_pass) VALUES ('" &request.form ("U_user") & "', '" & Request.Form ("U_pass") & "')"
%>
<table width= border= "0" align= "center" >
<tr>
<TD align= "center" ></td>
</tr>
</table>
</body>

Five, press F12 preview register.asp and enter data, click submit button



When you open the datasheet, user will see:

Description The operation was successful because it has been submitted two times, so we see two new records inserted in the table, but there are duplicate records in the table, the following is added to the code in result2.asp to resolve:
Copy Code code as follows:

<%@ Language=vbscript%>
<!--#include file= "con1.asp"-->

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Registration Success </title>
<style type= "Text/css" >
<!--
. STYLE1 {font-size:24px}
-->
</style>
<br><%
' Create a 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 "User name:" &request.form ("U_user") & "has been occupied, please click Back to register Again"
%>
<a href= "Register.asp" class= "STYLE1" > Return to Re-register </a>
<%
Response. End
Else
Set Rst1=conn.execute ("INSERT into User (U_user,u_pass) VALUES ('" &request.form ("U_user") & "', '" & Request.Form ("U_pass") & "')"
End If
%>
<table width= border= "0" align= "center" >
<tr>
&LT;TD align= "center" ></td>
</tr>
<tr>
&LT;TD align= "center" ><a href= "Register.asp" class= "STYLE1" > Return </a></td>
</tr>
</table>
</body>

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.