View Full Version : update last Record only
bmwmpower
04-09-2003, 10:04 AM
how i can update last Record only in the database
with an updatestatement:D
you can identify it by selecting the max(id) number or something, ot the record with the highest value in some sort of "editdate"-variable.
depends on your db structure and db-format. MySQL has some special features for that (if you use PHP).
How are you planning on identifying that record?
bmwmpower
04-09-2003, 12:21 PM
in my database i have some record for
1- autonamber >>>> namber
2- user name >>>> text
3- time login >>>> date time
4- date >>>> date time
5- time logout >>>> date time
my problem is when the user login to the website i insert the user name and timelogin and date and when he logout how i can update this record with time logout so what i need is update the record for the same user name how i can do that and this is the code
-------------------------
when user login
Set cn = Server.CreateObject("ADODB.Connection")
cn.ConnectionTimeout = 20
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("*****.mdb") & ";"
'SQLInsert = "Insert into report (Name , Timeadded , DateAdded , Node_cluster) values('" & session("temp") & "','" & time() & "','" & Date()& "','" & session("node")& "')" Set R = Server.CreateObject("ADODB.Recordset")
R.Open SQLInsert , cn
On error resume next
r.Close
cn.Close()
===========================
when he logout
Set cn = Server.CreateObject("ADODB.Connection")
cn.ConnectionTimeout = 20
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("*****.mdb") & ";"
SQLupdate = "update report set logout ='" & time() & "' where ????????? this is the problem "
Set R = Server.CreateObject("ADODB.Recordset")
R.Open SQLupdate , cn
On error resume next
r.Close
cn.Close()
================
i think i must use the id but how i can know the id for the user for are login
quite something different ;)
The easiest way is by storing the "autonamber"-value in a session variable.
So you need to retrieve that after the insert. There are currently 4 or 5 threads about retrieving the last inserted record. This (older) thread gives you the info you need.
http://www.codingforums.com/showthread.php?s=&threadid=17422&highlight=%40%40identity
then store the id in a sessionvcariable
session("userID")=rswhatever.Fields("autonamber")
When he logsout, update the record and use the value from the sessionvariable as condition
SQLupdate = "update report set logout ='" & time() & "' where autonamber=" & session("userID") &")"
bmwmpower
04-09-2003, 02:00 PM
how i can store the id for the same user in a sessionvcariable
how i can store the id for the same user in a sessionvcariable :confused: :confused:
it's in my previous post.
read the link to know how to retrieve the Id (autonamber) from the record you inserted (at the start of the session). You need to run a selectquery with the ID in the recordset (will be a recordset with one row and one variable)
session("userID")=rswhatever.Fields("autonamber")
this line of code then stores the value from the recordset in a sessionvariable
About the logout. Make sure that the records get updated before the sessions timed out !! or update these destroyed session with some sort of stored procedure
bmwmpower
04-09-2003, 02:45 PM
iam so sorry i don't understand the link so u have time to tell me the steps to store the id
There’s nothing to tell about that.
You just type in that one line. (alter the name of the variables and recordset if necessary)
The webserver will ‘remember’ that value for each client (=user). Its like a sort of seperate memoryspace (“instance of the session-object”) for each client. Whenever you then write variable=session(“userID’) the correct value for that client will be recoverd from the session-object and will be asigned to a variable.
The only tricky part is getting the right ID (“autonam’-variable in your table). The ID that you just inserted when the client started his session. This can be done by selecting the record that you inserted. You can use a datevalue or the username or those two combined to get the right record. This thread is quite readable if you keep this in mind.
An alternative (that’s a bit easier but harder on the server + bit more risky because when the server crashes, all running session are deleted) is just store all the info in sessionvariables. As soon as the user logs in, you store all the info you need in sessionvariables
Session(“user”)=request.form(“user”)
Session(“starttime”)=Now()
And when he logs out, you then just write it to the db.
sql="INSERT INTO table (name, started, finished) VALUES('thename',#time1#,#time2#)"
sql=replace(sql,"thename",session("user"))
sql=replace(sql,"time1",session("starttime"))
sql=replace(sql,"time2",Now())
Another alternative (bit more comple) is storing all the info from running session, in an array variable in the application-object
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.