...

View Full Version : Delete more than 1



christrinder
02-14-2003, 10:01 AM
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
%>

raven
02-14-2003, 11:27 PM
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
%>

whammy
02-15-2003, 12:20 AM
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. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum