Jag har frågat om detta förrut men hittar ej frågan bland alla inlägg jag postat så jag postar igen.
Jag har en databas där jag ska spara max 5 poster/medlem. När medlemmen lägger till en post ska det kollas i databasen. Om det redan finns 5 poster ska den gamlaste raderas å den nya sparas.
strSQL = "SELECT COUNT(poster) AS postcount FROM tblpost WHERE uid = " & Request.QueryString("user") & " ORDER BY postid ASC"
objRS.Open strSQL, objCon
If objRS("postcount") => 5 Then
i = 0
Do Until objRS.EOF OR i = 6
arrPost(i) = objRS("postid")
i = i + 1
Loop
strSQL = "DELETE FROM tblpost WHERE uid = " & Request.QueryString("user") & " AND postid = " & LBound(arrPost)
objCon.Execute(strSQL)
strSQL = "INSERT INTO tblpost (post) VALUES ('" & Request.Form("post") & "')"
Else
strSQL = "INSERT INTO tblpost (post) VALUES ('" & Request.Form("post") & "')"
End If
objCon.Execute(strSQL)
Response.Write("Posten är inlagd")
Det finns säkert många fel i koden eftersom jag inte kunnat testa
Skrivit såhär. FÖr man måste ju få med idnumret på nå sätt men då hittas inte pollId när man kör denna:
Set RS=MemPoll.Execute("Select pollId,Count(*) As postcount From fraga Where userId=" & Session("sex_id"))
If RS("postcount") => 5 Then
i = 0
Do Until RS.EOF OR i = 6
arrPost(i) = RS("pollId")
i = i + 1
Loop
MemPoll.Execute("Delete From fraga Where userId=" & Session("sex_id") & " And pollId=" & LBound(arrPost))
MemPoll.Execute("Insert Into fraga(fraga,userId) Values('" & Request.Form("fraga") & "'," & Session("sex_id") & ")")
Else
MemPoll.Execute("Insert Into fraga(fraga,userId) Values('" & Request.Form("fraga") & "'," & Session("sex_id") & ")")
End If
Response.Redirect "minsida.asp?id=" & Request.QueryString("id")
Microsoft JET Database Engine error '80040e21'
You tried to execute a query that does not include the specified expression 'pollId' as part of an aggregate function.
Jag la till Group By i sql strängen å då funkade det.
Set RS=MemPoll.Execute("Select pollId,Count(*) As postcount From fraga Where userId=" & Session("sex_id") & " GROUP BY pollId")
If RS("postcount") => 5 Then
i = 0
Do Until RS.EOF OR i = 6
arrPost(i) = RS("pollId")
i = i + 1
Loop
MemPoll.Execute("Delete From fraga Where userId=" & Session("sex_id") & " And pollId=" & LBound(arrPost))
MemPoll.Execute("Insert Into fraga(fraga,userId) Values('" & Request.Form("fraga") & "'," & Session("sex_id") & ")")
Else
MemPoll.Execute("Insert Into fraga(fraga,userId) Values('" & Request.Form("fraga") & "'," & Session("sex_id") & ")")
End If
Response.Redirect "minsida.asp?id=" & Request.QueryString("id")
MemPoll.Execute("Delete From fraga Where userId=" & Session("sex_id") & " And pollId=" & LBound(arrPost))
LBound(arrPost) är alltid 0.
mempoll.execute "delete from fraga " & _
" where userid = " & session("sex_id") & _
" and pollId not in (select top 5 pollId from fraga " & _
" where userId = " & session("sex_id") & _
" order by pollid desc,userid) "
Skulle det bara vara detta kvar om jag använder ditt exempel Lars?
MemPoll.Execute("Delete From fraga Where userid = " & session("sex_id") & " and pollId Not In (Select Top 5 pollId From fraga Where userId = " & session("sex_id") & " Order By pollid desc,userid)")
MemPoll.Execute("Insert Into fraga(fraga,userId) Values('" & Request.Form("fraga") & "'," & Session("sex_id") & ")")
Response.Redirect "minsida.asp?id=" & Request.QueryString("id")