Keeps adding new records

Results 1 to 2 of 2

Thread: Keeps adding new records

  1. #1
    Join Date
    Dec 1969

    Default Keeps adding new records

    The code below keeps adding the same value to a table in a database. Ie. The same name with a different ID.<BR><BR>Why is that? There&#039;s something wrong with the IF statement I presume.<BR><BR> trainername = request.form("name")<BR><BR> Set RS = Server.CreateObject("adodb.Recordset")<BR> Set RS2 = Server.CreateObject("adodb.Recordset")<BR><BR> conn = "DSN=eval"<BR><BR> SQL = "SELECT * FROM trainer"<BR><BR> SQL, conn<BR> <BR> DO WHILE not RS.eof<BR><BR> trainersname = RS("name") <BR><BR> IF trainersname = trainername THEN<BR> <BR> trainerID = RS("trainerID")<BR><BR> Else<BR><BR> SQL2 = "INSERT INTO trainer(name)"<BR> SQL2 = SQL2 & "VALUES(&#039;" & trainersname & "&#039;)"<BR><BR> SQL2, conn<BR><BR> End If<BR><BR> RS.MoveNext<BR><BR> LOOP <BR><BR><BR>The idea is that if the &#039;trainer&#039; exists already, then the ID is retrieved from the database for use elsewhere. Otherwise the name of the &#039;trainer&#039; is added to the database. I can&#039;t see anything wrong with the code, so any help would be appreciated.<BR><BR>Many thanks.

  2. #2
    Join Date
    Dec 1969

    Default Try

    SQL = "SELECT * FROM trainer Where name=&#039;" & request.form("name") & "&#039;"<BR><BR>Create an conn object using<BR><BR>set conn = Server.CreateObject("adodb.Connection")<BR>Conn.Op en "DSN=eval"<BR><BR><BR>Then do <BR>rs.Open SQL, Conn, adOpenStatic, adLockreadOnly, adCmdText <BR><BR>If Not RS.EOF Then<BR>trainerID = RS("trainerID")<BR>Else<BR>SQL2 = "INSERT INTO trainer(name)"<BR>SQL2 = SQL2 & "VALUES(&#039;" & trainersname & "&#039;)"<BR><BR>conn.execute SQL2<BR><BR>End If<BR><BR><BR> <BR>

Posting Permissions

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