PDA

View Full Version : Cookie values in db


karolmcauley
10-02-2002, 04:59 PM
Is it possible to store cookie values in a database field (Access) via an insert query?

For example, if i have set a cookie called username:

response.cookie("surname") = "username"

could i store that username value into a db, like:

INSERT INTO prediction (username,score,match,created_date)VALUES('" & username & "', '" & score & "', '" & match & "', '" & now() & "')"

If it is possible is the syntax right?

ManyThx in advance

martin_narg
10-02-2002, 11:47 PM
yes, it is possible, it would look something like this:


Dim connString, Connection, SQL
Dim username, score, match

username = "martin_narg"
score = "4-0"
match = "West Ham v Man Utd" 'and what a day that will be =)

' set cookie value
Response.Cookies("surname") = username

'database can be referred to relatively, ie databasefolder/myDatabase.mdb
'table in database is called prediction

' set database connection and open it
connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("myDatabase.mdb") & ";"
set Connection = Server.CreateObject( "ADODB.Connection" )
Connection.Open connString

' create query, using cookies("surname") as part of info to write to database
SQL = "INSERT into prediction (username, score, match, created_date)"
SQL = SQL & " VALUES( '" & Request.Cookies("surname") & "', '" & score & "', '" & match & "', '" & Date & "' )"

' execute query and close database
Connection.Execute( SQL )
Connection.Close


hope this helps

m_n

karolmcauley
10-03-2002, 11:26 AM
Thx for your suggestion......this is basically what i used to get it working in the end except i used the actual alias username value to insert into the db instead of what you have put ie. '" & Request.Cookies("surname") & "'. I assume your way works also as they are practically the same value.

On another point, how could i detect if a client computer has cookies disabled and thus display an error message hinting to turn cookies on before they proceed. I know there is a way, but it is just the technicalities of it.

If anyone has any script/tutorials/links etc on how to do this then i would be greatful.

Many thx in advance

glenngv
10-03-2002, 12:06 PM
this is a javascript forum but i think you need to detect cookies via ASP

' set cookie value
Response.Cookies("surname") = username

'check if cookie is enabled
if (Request.Cookies("surname")<>username) then
response.write("You need to enable cookies.")
response.end
end if