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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Loughborough (UK)
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Delete more than 1

    As a fairly newbie to ASP, I have started creating a forum (similar to this) as a learning exercise/project. I want users to be able to delete their post, but where they delete the first post in a thread, it then deletes all related posts. I've got the code below, but it only deletes the first one of the replies. I have tried adding a loop statement where not objRSreplies.EOF, but I get a error message saying that the operation cannot be completed whilst the object is closed? I need your help please!

    <%
    Dim deleteid
    deleteid = request.querystring("id")
    Set adoCon = Server.CreateObject("ADODB.Connection")
    adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("******.mdb")
    strSQLthread = "SELECT id, posttitle, posttime, postdate, post, postby, threadid from forum where id = "& deleteid &""
    Set objRSthread = adoCon.Execute(strSQLthread)
    If objRSthread("postby") <> session("forumuserid") then
    response.redirect "unauthorised.asp"
    End If
    %>

    <%
    Set objRSthread = Server.CreateObject("ADODB.Recordset")
    strSQLthread = "SELECT id, posttitle, posttime, postdate, post, postby, threadid from forum where id = "& deleteid &""
    objRSthread.CursorType = 2
    objRSthread.LockType = 3
    objRSthread.Open strSQLthread, adoCon
    objRSthread.Delete
    %>

    <%
    Set objRSreplies = Server.CreateObject("ADODB.Recordset")
    strSQLreplies = "SELECT id, posttitle, posttime, postdate, post, postby, threadid from forum where threadid = "& deleteid &""
    objRSreplies.CursorType = 2
    objRSreplies.LockType = 3
    objRSreplies.Open strSQLreplies, adoCon
    objRSreplies.Delete
    objRSreplies.MoveNext
    objRSreplies.Update
    objRSreplies.Close
    Set objRSreplies = Nothing
    %>

  • #2
    New Coder
    Join Date
    Jan 2003
    Location
    Peterborough, UK
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This seems a bit long winded, I'd do something like this:

    <%
    Dim deleteid
    deleteid = request.querystring("id")
    Set adoCon = Server.CreateObject("ADODB.Connection")
    adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("******.mdb")
    strSQLthread = "SELECT threadid, postby from forum where id = "& deleteid &""
    set objRSThread = Server.CreateObject("ADODB.Recordset")
    objRSThread.Open strSQLThread, adoCon,1,3

    If objRSthread("postby") <> session("forumuserid") then
    response.redirect "unauthorised.asp"

    ElseIf objRSThread.EOF
    'do something if no data

    Else
    dim idThread
    idThread = objRSThread("threadid")
    objRSThread.close
    set objRSThread = nothing
    Dim strSQLDelete
    strSQLDelete = "DELETE FROM forum WHERE threadid = " & idThread
    adoCon.Execute strSQLDelete

    End If
    %>

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is that in the WHERE clause you're using the ThreadID instead of the PostID (I'm assuming you have something like that as a related table)... therefore it deletes the whole thread...

    You must be VERY VERY careful about what you use in WHERE clauses when using delete (or even update)!

    If you don't use a correct WHERE clause, and you do a DELETE, it will delete EVERY record in your database. Same thing goes for UPDATE statements.

    FYI.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

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