Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a user to a databse

Status
Not open for further replies.

malladisk

Programmer
Jun 14, 2001
69
US
Hi!
I'm new to ASP and am trying to simulate a typical ecommerce site. I've got the register.asp and adduser.asp pages. The user fills in the register.asp and submits the data which is processed by adduser.asp. I'm following closely on the examply provided in "Beginning ASP 3.0" from Wrox press. For some reason I can't make out, my code doesn't work. :-( Can anyone help?
Here's the register.asp page:
Code:
<BASEFONT FACE=&quot;Comic Sans MS&quot; COLOR=&quot;DarkBlue&quot;>
<HTML>
<HEAD>
<SCRIPT language=&quot;JavaScript&quot;>
<!--
  function VerifyData()  // This just makes sure that that password and
  {                      // verify passwords values match.   
    if (document.frmUser.Password.value != document.frmUser.VerifyPassword.value)
    {
      alert (&quot;Your password and verified password do not match - please reenter&quot;);
      return false;
    }
    else
      return true;
  }
-->
</SCRIPT>

<TITLE>Sasikanth Malladi's Online Videostore - User Registration</TITLE>
</HEAD>
<BODY BGCOLOR=&quot;#FFFF80&quot;>
<CENTER>
<% 
  If Request(&quot;Update&quot;) = &quot;True&quot; Then 
    Response.Write &quot;<H1>Video Store<BR> Update User Registration</H1>&quot;
  Else 
    Response.Write &quot;<H1>Video Store<BR> New User Registration</H1>&quot;
  End If 
%>
</CENTER>
<P>

<% 
' The following piece of code checks for info update
' or registration.
  If Request(&quot;Update&quot;) = &quot;True&quot; Then 
    Response.Write &quot;Please change your registration information as listed below<P>&quot;
  Else 
    If Request(&quot;NotFound&quot;) = &quot;True&quot; Then 
      Response.Write &quot;<I>We were unable to locate your information. &quot; & _
                     &quot;Please take the time to register again.</I><P>&quot;
    Else 
      Response.Write &quot;<CENTER>(If you're already registered with us, &quot; & _
                     &quot;then click the 'Login' link below.)</CENTER><P>&quot;
    End If 
    Response.Write &quot;You only need to register with our system if you want to &quot; & _ 
                   &quot;buy videos with us. <BR> &quot; & _
                   &quot;In order to use these services, please take a few minutes &quot; & _
                   &quot;to complete the form below. Once you have done that, &quot; & _
                   &quot;you will have full access to the system.&quot;
  End If 
%>

<FORM ACTION=&quot;AddUser.asp&quot; NAME=&quot;frmUser&quot; METHOD=&quot;POST&quot; onSubmit=&quot;return VerifyData()&quot;>
  <TABLE BORDER=0>
    <TR>
      <TD WIDTH=20% ROWSPAN=11> </TD>
      <TD>E-Mail Address:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;email&quot; VALUE=&quot;<%= Session(&quot;EMailAddress&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>Given Name:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;GivenName&quot; VALUE=&quot;<%= Session(&quot;GivenName&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>Family Name:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;FamilyName&quot; VALUE=&quot;<%= Session(&quot;FamilyName&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>Address:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;Address1&quot; VALUE=&quot;<%= Session(&quot;StreetAddress1&quot;)%>&quot; 
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD></TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;Address2&quot; VALUE=&quot;<%= Session(&quot;StreetAddress2&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>City:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;City&quot; VALUE=&quot;<%= Session(&quot;City&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>State:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;State&quot; VALUE=&quot;<%= Session(&quot;State&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>Postal Code:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;PostalCode&quot; VALUE=&quot;<%= Session(&quot;PostalCode&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>Country:</TD>
      <TD><INPUT TYPE=&quot;Text&quot; NAME=&quot;Country&quot; VALUE=&quot;<%= Session(&quot;Country&quot;)%>&quot;
           SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD> <P>Password:</TD>
      <TD VALIGN=bottom><INPUT TYPE=&quot;Password&quot; NAME=&quot;Password&quot;  SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      <TD>Verify Password:</TD>
      <TD><INPUT TYPE=&quot;Password&quot; NAME=&quot;VerifyPassword&quot; SIZE=&quot;40&quot;></TD>
    </TR>
    <TR>
      
      <TD ALIGN=CENTER COLSPAN=2><BR>
          <INPUT TYPE=&quot;Submit&quot; VALUE=&quot;Submit Registration&quot;>
                <INPUT TYPE=&quot;RESET&quot;></TD>
    </TR>
  </TABLE>
</FORM>

<HR><CENTER>
<TABLE WIDTH=80%><TR><TD>
<A HREF=&quot;Default.asp&quot;> Home </A> </TD> <TD><A HREF=&quot;login.asp&quot;> Login </A></TD>
</TABLE></CENTER>
</BODY>
</HTML>
[bold]And here's the adduser.asp page: [/bold]
Code:
<%'!--#include file=&quot;Datastore.asp&quot;--%>


<%
Dim rsUsers ' Declaring a record set object of users
' This will be used to interact with the User table
Set rsUsers = Server.CreateObject (&quot;ADODB.Recordset&quot;)
 ' and initializing it with the server object method
 
 rsUsers.Open &quot;Person&quot;,objConn,adOpenForwardOnly,adLockOptimistic,adCmdTable
 ' The Open method will populate the record set with the User
 ' table. adLockOptimistic allows us to write info to the
 ' table.adOpenForwardOnly allows to only move forward in the table
 
 
If Session(&quot;PersonID&quot;) <> &quot;&quot; Then  ' currently logged on user
 rsUsers.Filter = &quot;PersonID= '&quot; & Session(&quot;PersonID&quot;) & &quot;'&quot;
 ' set the filter so that only this person's record is open
 ' this filter is set by the Filter property of the RS object
 ' This is done by getting the Session's PersonID and filtering
 ' the RS using this PersonID
 Else ' If Session(&quot;PersonID&quot;) is not available, use the email addr
 ' and password to set the filter.
 rsUsers.Filter = &quot;EMailAddress '&quot; & Request.Form(&quot;email&quot;) & &quot;'&quot; & _
                  &quot;AND Password = '&quot; & Request.Form (&quot;Password&quot;) & &quot;'&quot;
                  
   If rsUsers.EOF Then
   ' if email addr and password not found, add new user.
       rsUsers.AddNew 
  ' Else
  ' Email address and password matched with DB records                  
  ' In this case we'll the user to allow personal details
   End If
End If

' Now write the details to the Person table
rsUsers(&quot;EMailAddress&quot;) = Request.Form (&quot;email&quot;)
rsUsers(&quot;Password&quot;) = Request.Form (&quot;Password&quot;)
rsUsers(&quot;GivenName&quot;) = Request.Form (&quot;FamilyName&quot;)
rsUsers(&quot;FamilyName&quot;) = Request.Form (&quot;GivenName&quot;)
rsUsers(&quot;StreetAddress1&quot;) = Request.Form (&quot;Address1&quot;)
rsUsers(&quot;StreetAddress2&quot;) = Request.Form (&quot;Address2&quot;)
rsUsers(&quot;City&quot;) = Request.Form (&quot;City&quot;)
rsUsers(&quot;State&quot;) = Request.Form (&quot;State&quot;)
rsUsers(&quot;PostalCode&quot;) = Request.Form (&quot;PostalCode&quot;)
rsUsers(&quot;Country&quot;) = Request.Form (&quot;Country&quot;)
rsUsers(&quot;Active&quot;) = True
rsUsers(&quot;LastLogin&quot;) = Now
rsUsers.Update 
' Done. Update the table
' Now write all this info into the session level variables

Dim strName, strValue
' Each RS object has its own Fields collections, which corresponds to the
' fields in the database table which we're currently referring to. This
' fields collection corresonds to the record we're referring to in the table.
For each strField in rsUsers.Fields
strName = strField.Name ' Name of the field. PersonID, email address, etc.
strValue = strField.value ' and the corresponding values.
Session(strName) = strValue ' put them into the current session object
' Now the user is logged-in and an active session is in progress.

Next  ' end for

Session(&quot;blnValidUser&quot;) = True 'valid current user.
Response.Redirect &quot;MenuForRegisteredUsers.asp&quot;
' Now for what all a regd user can do!
%>
and here's the datastore.asp:
Code:
<!-- METADATA TYPE=&quot;typelib&quot; 
              FILE=&quot;C:\Program Files\Common Files\System\ado\msado15.dll&quot; -->
<!-- The above file contains all the constants used as parameters when 
     working with the ADODB objects. -->
<%
  Dim objConn'  'declare a variable that will hold the DB connection
  Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
  ' and create the connection object
  objConn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; & _
               &quot;Data Source= C:\Inetpub\[URL unfurl="true"]wwwroot\smalladi\videostore\Movie2000.mdb&quot;[/URL]
 
  ' blnValidUser is a Session variable that's True if a person is logged in
  ' i.e. an active session.
  If Session(&quot;blnValidUser&quot;) = True and Session(&quot;PersonID&quot;) = &quot;&quot; Then
  ' if session is active and PersonID is blank, then fetch that person's info
    Dim rsPersonIDCheck ' create a record set to hold that person's info
    Set rsPersonIDCheck = Server.CreateObject(&quot;ADODB.Recordset&quot;)
    Dim strSQL 
    strSQL = &quot;SELECT PersonID FROM Person &quot; & _ 
             &quot;WHERE EMailAddress = '&quot; & Session(&quot;EMailAddress&quot;) & &quot;';&quot;
    rsPersonIDCheck.Open strSQL, objConn ' populate the RS object
    If rsPersonIDCheck.EOF Then
    ' if the email is invalid, we log out the person
      Session(&quot;blnValidUser&quot;) = False
    Else
      Session(&quot;PersonID&quot;) = rsPersonIDCheck(&quot;PersonID&quot;)
    End If
    rsPersonIDCheck.Close
    Set rsPersonIDCheck = Nothing
  End If
%>
 
Hi, Vasah!
Thanks for your attempt but I've managed to fix it. The worst thing was I wasn't getting any error msg so I had big trouble fixing it. I just used to get 500 Internal Server Error.
In fact, it was a pretty mundane error. My field names in my database were incorrectly accessed when trying to write data.
Now that you're here, can you give me the idea how to implement a shopping cart? I just need the idea outline. The book I'm using now (Beginning ASP 3.0) doesn't say anything about how to do this.
Thanks,
Seshi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top