1 / 19

DT221/3 Internet Application Development

DT221/3 Internet Application Development. Active Server Pages & Database Connection. Example of ASP Database Connection. A simple application example Use ASP for HTML form Login Check Database Connection Database Insertion, Update and Deletion. Login Form. Insertion. User Management.

dea
Download Presentation

DT221/3 Internet Application Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DT221/3 Internet Application Development Active Server Pages & Database Connection

  2. Example of ASP Database Connection • A simple application example • Use ASP for HTML form • Login Check • Database Connection • Database Insertion, Update and Deletion

  3. Login Form Insertion User Management Action Process Update Insertion Form Update Form Deletion Conform Deletion Example of ASP Database Connection

  4. Example of ASP Database Connection-login.asp <%@ Language=VBScript %> <HTML> <HEAD><title>Login page</title> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <form name=form1 method="post" action="check.asp"> User Name: <input type="text" id="user" name="user"> <p> Password: <INPUT type="password" id="password" name="password"> <p> <INPUT type="reset" value="Reset" id=reset name=reset>&nbsp;&nbsp;&nbsp;&nbsp; <INPUT type="submit" value="Login" id=submit name=submit> </form> </BODY> </HTML>

  5. Example of ASP Database Connection-check.asp(1) <%@ Language=VBScript %> <% Dim user, password user = Request.Form("user").Item password = Request.Form("password").Item dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs.ActiveConnection = Conn mySQL = "Select SecurityLevel From cliu.users Where UserName= '" & user & "' and Password = '" & password & "'" Rs.Open(mySQL)

  6. Example of ASP Database Connection-check.asp(2) if not Rs.eof then if Rs("securityLevel")=12 then Session("level")=12 Conn.Close Set Conn = NOTHING Response.Redirect ("menu.asp") end if if Rs("securityLevel")=8 then Session("level")=8 Conn.Close Set Conn = NOTHING Response.Redirect("showuser.asp") end if if Rs("securityLevel")=1 then Session("level")=1 Conn.Close Set Conn = NOTHING Response.Redirect("welcome.asp") end if

  7. Example of ASP Database Connection-check.asp(3) else Session("level")=-1 Conn.Close Set Conn = NOTHING Response.Redirect("login.asp") end if %>

  8. Example of ASP Database Connection-check.asp(3) else Session("level")=-1 Conn.Close Set Conn = NOTHING Response.Redirect("login.asp") end if %>

  9. Example of ASP Database Connection-adduser.asp <%@ Language=VBScript %> …… <p><form name=form1 method="post" action="addusertodb.asp"> Please enter the new user name: <input type="text" id="user" name="user" size="20"> <p>Please enter the new password: <INPUT type="password" id="password" name="password" size="20"> <p>Please enter the first name: <input type="text" id="first" name="first" size="20"> <p>Please enter the second name: <input type="text" id="second" name="second" size="20"> <p>Please enter the security level: <input type="text" id="level" name="level" size="20"> <p><INPUT type="reset" value="Reset" id=reset name=reset>&nbsp;&nbsp;&nbsp;&nbsp; <INPUT type="submit" value="Add" id=submit name=submit> </form> </BODY> </HTML>

  10. Example of ASP Database Connection-addusertodb.asp <%@ Language=VBScript %> <%Dim user, password, first, second, level user = Trim(Request.Form("user").Item) password = Trim(Request.Form("password").Item) first = Trim(Request.Form("first").Item) second = Trim(Request.Form("second").Item) level = Request.Form("level").Item dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") mySQL = "Insert into cliu.users values ( '" & user& "' , '" & password & "', '" & first & "', '" & second &"' , " & level & ")“ Conn.Execute(mySQL) Conn.Close Set Conn = NOTHING Response.Redirect("showuser.asp") %>

  11. Example of ASP Database Connection-edit.asp <%@ Language=VBScript %> …… <% Response.Write("Please select the user to edit:<p>") dim Conn, Rs, user_id, i set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs.ActiveConnection = Conn Rs.Open("Select * From cliu.users") While not Rs.eof Response.Write("user name = " &Rs("UserName").Value & " ") Response.Write("<a href=edituser.asp?id=" & Rs("UserID").Value & ">edit</a>") Response.Write("<br>") Rs.MoveNext Wend %> </body> </html>

  12. Example of ASP Database Connection-edituser.asp(1) <%@ Language=VBScript %> <%Dim userid userid = Int(Request.QueryString("id").Item) dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs = Conn.Execute("cliu.selectuser " &userid) %> <HTML> <HEAD><title>Select user to edit</title> <META NAME="GENERATOR" Content="Microsoft FrontPage 6.0"> </HEAD> <BODY>

  13. Example of ASP Database Connection-edituser(2).asp <SCRIPT language=Javascript> //Client side javascript function check(){ test=true if (document.form1.user.value==""){ alert("Please enter the user name"); document.form1.user.focus(); test=false; } if (document.form1.password.value==""){ alert("Please enter the password"); document.form1.password.focus(); test=false; } if (document.form1.first.value==""){ alert("Please enter the user' first name"); document.form1.first.focus(); test=false; } if (document.form1.second.value==""){ alert("Please enter the user's second name"); document.form1.second.focus(); test=false; }

  14. Example of ASP Database Connection-edituser(3).asp if (document.form1.level.value==""||(document.form1.level.value!=12&&document.form1.level.value!=8&&document.form1.level.value!=1)){ alert("Please enter the correct level"); document.form1.user.focus(); test=false; } return test } </SCRIPT> <form name=form1 method="post" action=editusertodb.asp onSubmit="return check()"> <input type="hidden" name=userid id=userid value=<%=userid%>>

  15. Example of ASP Database Connection-edituser.asp(4) Please enter the new user name: <input type="text" id="user" name="user" value="<%=Rs("UserName").Value%>" size="20"> <p>Please enter the new password: <INPUT type="password" id="password" name="password" value="<%=Rs("Password").Value%>" size="20"> <p>Please enter the first name: <input type="text" id="first" name="first" value="<%=Rs("FirstName").Value%>" size="20"> <p>Please enter the second name: <input type="text" id="second" name="second" value="<%=Rs("SecondName").Value%>" size="20"> <p>Please enter the security level: <input type="text" id="level" name="level" value="<%=Rs("SecurityLevel").Value%>" size="20"> <p><INPUT type="reset" value="Reset" id=reset name=reset>&nbsp;&nbsp;&nbsp;&nbsp; <INPUT type="submit" value="Edit" id=submit name=submit> </form></BODY> </HTML>

  16. Example of ASP Database Connection-editusertodb.asp <%@ Language=VBScript %> <%Dim user, password, first, second, level userid = Int(Request.Form("userid").Item) user = Trim(Request.Form("user").Item) password = Trim(Request.Form("password").Item) first = Trim(Request.Form("first").Item) second = Trim(Request.Form("second").Item) level = Int(Request.Form("level").Item) dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Conn.Execute("cliu.edituser "& userid &",'" & user& "' , '" & password & "', '" & first & "', '" & second &"' , " & level) Conn.Close Set Conn = NOTHING Response.Redirect("menu.asp") %>

  17. Example of ASP Database Connection-deluser.asp(1) <%@ Language=VBScript %> <html> <head><title>Database Connection</title> <script> function ConfirmDelete(s_User, s_ID) { var s_Dialog = "Are you sure you want to delete user '" + s_User + "'?"; if(confirm(s_Dialog)) { document.location.href = "deluserdb.asp?id=" + s_ID; } }// end function </script> </head> <body>

  18. Example of ASP Database Connection-deluser(2).asp <% Response.Write("Please select the user to delete:<p>") dim Conn, Rs, user_id, i set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Rs.ActiveConnection = Conn Rs.Open("Select * From cliu.users") While not Rs.eof Response.Write("user name = " &Rs("UserName").Value & " ") Response.Write("<a href=""javascript:ConfirmDelete('" & Rs("UserName").Value & "','" &Rs("UserID").Value & "')"">del</a>") Response.Write("<br>") Rs.MoveNext Wend %> </body> </html>

  19. Example of ASP Database Connection-deluserdb.asp <%@ Language=VBScript %> <% Dim userid userid = Int(Request.QueryString("id").Item) dim Conn, Rs, mySQL set Conn = Server.CreateObject("ADODB.Connection") ' conn objects set Rs = Server.CreateObject("ADODB.RecordSet")' Record Set Conn.Open ("Provider=sqloledb;Server=CIAN;Database=cliu;Uid=testuser;Password=;") Conn.Execute("cliu.deluser "& userid ) Conn.Close Set Conn = NOTHING Response.Redirect("menu.asp") %>

More Related