Hey... I Remember You! - Adding new users
(Page 3 of 7 )
To add a new user, we will create a new ASP script named adduser.asp. Fire up notepad and enter the following code (Ignoring the line numbering):
<html>
<head>
<title> Add User </title>
</head>
<body bgcolor="#ffffff">
<form action="adduser.asp" method="post">
<input type="hidden" name="what" value="addnew">
<h1>Add New User</h1>
<pre>
Username: <input type="text" name="uname" maxlength="20">
Password: <input type="password" name="pass" maxlength="20">
<br><input type="submit" value="Add User">
</pre>
</form>
</body>
</html>We've just created a pretty simple HTML form. A couple of things to note though: The form is posting the results to adduser.asp, which means that we are posting to the same script. We are also using a hidden input tag to set the value of a request variable named "what" to "addnew".
It's a common practice these days to post the results of a form back to the page that called it and distinguish what the page should display using hidden form variables. In our example above, the hidden "what" variable will be used to tell our script that we want to add a new user. To workout whether or not we are displaying the form to collect the users details, or actually adding the user to our database, we will use a select case statement. Add the following code to the top of your adduser.asp file.
1. <%
2.
3. dim strWhat
4. strWhat = Request.Form("what")
5.
6. select case strWhat
7. case "addnew"
8. ProcessUser()
9. case else
10. ShowUserForm()
11. end select
12.
13. sub ShowUserForm()
14. ...
...
34. end sub
35.
36. sub ProcessUser()
37.
38. dim objConn
39. dim strUserName
40. dim strPassword
41.
42. set objConn = Server.CreateObject("ADODB.Connection")
43. strUserName = Request.Form("uname")
44. strPassword = Request.Form("pass")
45.
46. objConn.Open "Provider=SQLOLEDB; Data Source=localhost; Initial Catalog=smartlogin; UId=sa; Pwd="
47.
48. objConn.Execute "INSERT INTO logins(user_name, password) VALUES('" & strUserName & "', '" & strPassWord & "')"
49.
50. Response.Write "New user " & strUserName & " added OK."
51.
52. end sub
53. %>The code above splits our adduser.asp page into two sub routines. On line 4 we're setting the variable that will tell our script what to do. If a form is being posted, the Request.Form(“what”) key will contain the value “adduser”. If not, it will contain nothing, meaning that there is no form posted and that we should call our ShowUserForm sub that will display the form.
On the other hand, if the Request.Form(“what”) variable contains the value “adduser”, we will call the ProcessUser sub routine which uses an ADO connection object to connect to our database and execute an INSERT query. This INSERT query takes the username and password values from the form and inserts them into a new row in our logins table, thus creating a new user. Lastly, our ASP script simply prints out “New user John added OK.” (Assuming the name of the user we added was John).
Note that for simplicity, all error checking is omitted from the sample we're creating. Now that we can add users to our logins table, we will want to let the users login and “remember them”.
Next: Creating the login page >>
More ASP Articles
More By Mitchell Harper