Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to check if data exists, it yes, do not save the form

    Hello,
    I am trying to create a form to insert data in sql server table. I want to check if email address exists or not. If exists, shows errors like "Email exists, try another", if not, insert the form to the db.

    Here is my code, but it does nothing.
    Is there a better way to check it or what did I do wrong?
    thanks.

    email_address = trim(fixStr(Request("emailAddress")))
    first_name = trim(fixStr(Request("firstName")))

    <%
    strSQL = "Insert into students(First_name, email_address) values ('" & First_Name & "', '" & Email_Address & "')"

    strSQL = "Select * From students Where Email_Address = '" & email_Address & "'"
    set pRS = server.CreateObject("ADODB.Recordset")
    pRS.open strSQL, Conn,3,1,1
    if not(pRS.eof) then
    pRS.close
    set pRS = nothing
    conn.close
    set conn=nothing
    session("strMsg") = "The Email address existed, try another"
    response.redirect "student.asp?FirstName=" & First_Name &" &EmailAddress=" & Email_Address
    end if
    conn.execute strSQL

    %>

  • #2
    New Coder
    Join Date
    Jan 2006
    Location
    South Central, KS
    Posts
    81
    Thanks
    2
    Thanked 2 Times in 2 Posts
    what's not working? are the new records not inserting. if that's the case it may be because you are never actually running the insert in the code you showed.

    if your search is not finding anything, then i would suggest you check to make sure that you have all of the field names correct.

    otherwise it would say it should be working.

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This code will do the checking for email if exists:

    set pRS = server.CreateObject("ADODB.Recordset")
    pRS.open strSQL, Conn,3,1,1
    if not(pRS.eof) then
    pRS.close
    set pRS = nothing
    conn.close
    set conn=nothing
    session("strMsg") = "The Email address existed, try another"
    response.redirect "student.asp?FirstName=" & First_Name &" &EmailAddress=" & Email_Address
    end if

    If I remove the block above, it inserted the data. But when I place it in to check email exists or not, then it does not insert anything, but don' cause errors.

    can you help please?

    thanks.

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    You are over writing your strSQL variable.

    Just declare the insert sql string after checking for the record. So you want:

    Code:
    strSQL = "Select * From students Where Email_Address = '" & email_Address & "'"
    set pRS = server.CreateObject("ADODB.Recordset")
    pRS.open strSQL, Conn,3,1,1
    if pRS.eof then
    	pRS.close
    	set pRS = nothing
    	conn.close
    	set conn=nothing
    	session("strMsg") = "The Email address existed, try another"
    	response.redirect "student.asp?FirstName=" & First_Name &" &EmailAddress=" & Email_Address
    end if
    strSQL = "Insert into students(First_name, email_address) values ('" & First_Name & "', '" & Email_Address & "')"
    conn.execute strSQL
    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    it worked.
    thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •