Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2003
    Posts
    7

    Unanswered: How to check if record in database exists?

    Hi ppl, I'm trying to make a program where users fill out a form and submit.I've no problems inserting data into the database(sql 2000)

    However, records must be unique.
    Example: Category1 + Category2 + Category3 must be unique in the database.

    So how do I check when users fill in the same data and submit to the database?
    I'd like to inform users that the record already exists.

    Thanks in advance!

  2. #2
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    6

    Talking

    I am not quite sure what do you mean by unique "Category1 + Category2 + Category3".

    However, if you just want to make sure the same data doesn't go into the database twice, can't you just first query for the data that the user try to put in, and if it returns nothing(meaning the data is not in the db), then you put it in?

  3. #3
    Join Date
    Aug 2003
    Posts
    7
    Oh let me try to explain.
    I have a table named Category in a Database with fields Cat1,Cat2,Cat3.

    Lets say there is a record already in the database with Cat1=a, Cat2=b, Cat3=c.

    And a user tries to submit a form with the exact same values, I wish to query the database and check the values in the fields entered.
    I'm not too sure how to go about it.The following is codes that check ONE field only and I still cant get it to work.

    rsSQL = "SELECT Category1 FROM Category WHERE Category1 = '" & cat1 & "'"
    set rs=Server.CreateObject("ADODB.recordset")
    rs.Open rsSQL, DB

    Do While Not rs.EOF
    if rs("Category1") = cat1 then
    strExist = "yes"
    strCat1 = rs("category1")
    end if
    rs.MoveNext
    Loop

    rs.close
    set rs=nothing

    If strExist = "yes" then
    response.write ("Record Exists.<a href='javascript: history.back()'>Back</a>")
    response.write(strCat1)
    end if

  4. #4
    Join Date
    Jan 2003
    Location
    Midwest
    Posts
    138
    Code:
    
    rsSQL = "SELECT COUNT(*) As count FROM Category WHERE Category1 = '" & cat1 & "' AND Category2 = '" & cat2 & "' AND Category3 = '" & cat3 & "'"
    set rs=Server.CreateObject("ADODB.recordset")
    rs.Open rsSQL, DB
    
    if(rsSQL("count") > 0) then
         strExists = "yes"
    else
         strExists = "no"
    end if
    
    rs.close
    set rs=nothing
    
    If strExist = "yes" then 
    response.write ("Record Exists.<a href='javascript: history.back()'>Back</a>")
    response.write(strCat1)
    end if

  5. #5
    Join Date
    Aug 2003
    Posts
    7
    Woohoo I modified the codes a little and now it works~
    Thanks for all your help! ^^

Posting Permissions

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