ASP form to check weather a form value is already in the database

This is a discussion on "ASP form to check weather a form value is already in the database" within the Classic ASP section. This forum, and the thread "ASP form to check weather a form value is already in the database are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Classic ASP

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Oct 18th, 2007, 19:37
Up'n'Coming Member
Join Date: Jul 2007
Location: Barry
Age: 22
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
ASP form to check weather a form value is already in the database

i have made a script that gets the values from a form and adds it to a database but what i now want to do is add a check so if the value is already in the data base it will say Username is already in use please select another so if for instence some one fills out the username as tom and another person then trys to use tom as a username it will say that the username is already in use here is my process code

<%
' declare variables
Dim UserName
Dim Password
Dim Email
' get posted data into variables
UserName = Trim(Request.Form("UserName"))
Password = Trim(Request.Form("Password"))
Email = Trim(Request.Form("Email"))
' validation
Dim validationOK
validationOK=true
If (Trim(UserName)="") Then validationOK=false
If (Trim(Password)="") Then validationOK=false
If (Trim(Email)="") Then validationOK=false
IF (Trim(UserName)="EOF" or Trim(Password)="EOF" or Trim(Email)="EOF") Then validationOK=false & response.write("If you have entered <strong>EOF</strong> in any of the fields then.<br>")
If (validationOK=false) Then
Response.write("Please go back fill in all ")
Response.write("the requied information<br>")
Response.write("<a href=""signup.asp"">Click here</a> to return to the form.")
ELSE
'Add account to Database
Dim sConnection, objConn , objRS , strSQL
sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=xxxx; UID=xxxx;PASSWORD=xxxx; OPTION=3"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(sConnection)
Set objRS = objConn.Execute("INSERT INTO accounts (user,password,email ) VALUES ('" & username & "','" & password & "','" & email & "')")
'Response.Redirect("index.asp")
Response.write("<h2>Account Added!</h2>")
Response.write("Account: " & UserName & "<BR>")
Response.write("Password: " & Password & "<BR>")
Response.write("Email: " & Email)
END IF
%>

that form makes sure the vales from the form are not blank but i want it to check there not blank and the values dont already exist in the database.. any one help me
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 19th, 2007, 05:26
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,611
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 3 Posts
Re: ASP form to check weather a form value is already in the database

maybe this code might help you but if it don't, please let me know.

Code: Select all
    do while not objRS.EOF
        if rs("username")=Username then ' this will check if the entered username has already exist in the database
            set objRS=nothing
            set objConn=nothing
            Response.Redirect("UserNameAlreadyExist.asp")
        end if
        objRS.MoveNext
    loop
Last Blog Entry: ASP Programming Tips and Technique (Oct 26th, 2007)

Last edited by Monie; Oct 19th, 2007 at 05:29.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 25th, 2007, 07:49
Up'n'Coming Member
Join Date: Aug 2007
Location: Haverhill
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Re: ASP form to check weather a form value is already in the database

I'd have thought that you would need to create a second query of the database.

Something like
Code: Select all
Dim Exists
Exists = false
Set objRS = objConn.Execute("SELECT * FROM accounts WHERE user = <%=UserName%>")
    do while not objRS.EOF
        Exists = true
        objRS.MoveNext
    loop
    set objRS=nothing
    set objConn=nothing
Then change your ELSE Statement to an ELSE IF something Like

Code: Select all
ELSE IF Exists
'Enter code to deal with trying to enter a username that already exists
ELSE
'Enter the code you already have for adding the account
END IF
END IF
Please feel free to explain why this is rubbish or won't work monie or anyone else.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 25th, 2007, 08:23
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,611
Blog Entries: 2
Thanks: 0
Thanked 4 Times in 3 Posts
Re: ASP form to check weather a form value is already in the database

Code: Select all
 <%
 ' declare variables
Dim UserName
Dim Password
Dim Email

 ' get posted data into variables
UserName = Trim(Request.Form("UserName")) 
Password = Trim(Request.Form("Password")) 
Email = Trim(Request.Form("Email")) 

 ' validation
Dim validationOK
validationOK=true

If (Trim(UserName)="") Then
     validationOK=false
Else If (Trim(Password)="") Then
     validationOK=false
Else If (Trim(Email)="") Then
     validationOK=false
Else IF (Trim(UserName)="EOF" or Trim(Password)="EOF" or Trim(Email)="EOF") Then
     validationOK=false & response.write("If you have entered <strong>EOF</strong> in any of the fields then.<br>")

Else If (validationOK=false) Then 
     Response.write("Please go back fill in all ")
     Response.write("the requied information<br>")
     Response.write("<a href=""signup.asp"">Click here</a> to return to the form.")

Else If
  1. "create another database connection here to query all the data in your database"
  2. perform the validation as follow:
do while not rs.EOF if rs("username")=Username Then set rs=nothing set conn=nothing Response.Redirect("username_duplicated.asp") end if rs.MoveNext loop Else 'Add account to Database
Dim sConnection, objConn , objRS , strSQL sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=xxxx; UID=xxxx;PASSWORD=xxxx; OPTION=3" Set objConn = Server.CreateObject("ADODB.Connection") objConn.Open(sConnection) Set objRS = objConn.Execute("INSERT INTO accounts (user,password,email ) VALUES ('" & username & "','" & password & "','" & email & "')") 'Response.Redirect("index.asp") Response.write("<h2>Account Added!</h2>") Response.write("Account: " & UserName & "<BR>") Response.write("Password: " & Password & "<BR>") Response.write("Email: " & Email)
End If %>
Hey Phil, I think your code will work but a little bit longer tho.. but hey, I learn something from your code today. Maybe I'll use it some other day..

Thanks.
Last Blog Entry: ASP Programming Tips and Technique (Oct 26th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Add form components to a database griffonwing Classic ASP 2 Aug 18th, 2007 17:17
form database Barbs7349 Starting Out 1 Aug 12th, 2007 13:22
Check for valid email in JMail form palgraphpres Classic ASP 1 Apr 5th, 2006 19:43
Submitting Web form results to a database theproman23 Databases 2 Jun 30th, 2005 13:21
insert form to database Monie Databases 1 Aug 25th, 2004 02:59


All times are GMT. The time now is 15:06.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42