16. februar 2008 - 02:07Der er
36 kommentarer og 1 løsning
Hente værdier fra to tabeller.
Jeg har to tabeller; 'profiler' og 'mails'
På min side (post.asp) starter jeg med at skrive: ------------------------------------------------------ <% ' Databaseforbindelse oprettes Set Conn = Server.CreateObject("ADODB.Connection") DSN = "DRIVER={Microsoft Access Driver (*.mdb)}; " DSN = DSN & "DBQ=" & Server.MapPath ("f_date_db.mdb") Conn.Open DSN
' Hent fra databasen afhængig af værdien id fra URL strSQL = "Select * From mails Where mail_owner = " & Request.Querystring("id")
Set rs = Conn.Execute(strSQL) %> --------------------------------------------------------
Længere nede udskriver jeg på siden: -------------------------------------------------------- <% if rs.EOF or rs.BOF then response.write "<center><p><font face='Verdana' size='2' color='#FFFFFF'>Du har ingen post i din indbakke!</font></p>" else
rs.MoveNext i = i+1 Loop While Not rs.EOF and i<16
end if Conn.Close Set Conn = Nothing %> --------------------------------------------------------
Det er som sådan også fint nok, alt fungere. Bortset fra at afsender navnet (mail_from) bliver udskrevet som 'id' nummeret. Det passer selvfølgelig overens med, at i tabellen 'mails' har den listed alt under 'mail_from' som id-numre.
Mit spørgsmål er så. Hvordan skriver jeg en kode, så den istedet for at bare udskrive 'mail_from' men istedet går ind og læser i tabellen 'profiler' og finder, udfra 'id', afsenderens navn?
Det skal lige siges, at afsenderes navn er listet under 'p_navn' i tabellen 'profiler'
Kan ikke helt få den til at fungerer. Jeg skal erstatte min: 'strSQL = "Select * From mails Where mail_owner = " & Request.Querystring("id")'
med din: 'strSQL = "SELECT mails.*, profiler.Navn FROM mails INNER JOIN profiler ON mails.ID = profiler.ID Where mail_owner = " & Request.Querystring("id")'
ikk? - så kommer den med denne fejl-meddelser: --------------------------------------------------- Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
/k2j/jefjor/brock2/post.asp, line 18 ---------------------------------------------------- Jeg skriver lige for en go ordens skyld hvad jeg har i de to tabeller: mails: mail_id, mail_owner, mail_from, mail_subject, mail_body profiler: id, p_navn
i have'nt made any relation between the two tables..
but if i relate field 'id' in 'profiler' with field 'mail_id' in 'mails' it shows the page 'post.asp' with the result: "Du har ingen post" (You have no mail) it doesn't show the mail
Which field in mails can be used to find the name in profiler?
When I look at the original SQL ("Select * From mails Where mail_owner = " & Request.Querystring("id")) it looks as though mail_owner is what you want to see so I would have though that this would also be used to find the actual name in profiler?
This one you just gave me works, however it is still the 'id' which is written on the page, not the name.. i will try the other original SQL again.. 2 sec
if i use the original one: strSQL = "SELECT mails.*, profiler.Navn FROM mails INNER JOIN profiler ON mails.ID = profiler.ID Where mail_owner = " & Request.Querystring("id")
then is says: "Du har ingen post i indbakken" (you have no mails)
ops i mean this one: strSQL = "SELECT mails.*, profiler.p_navn FROM mails INNER JOIN profil ON mails.mail_id = profiler.id Where mail_owner = " & Request.Querystring("id")
Morning. OK, so the SQL works but now you need to alter the ASP/HTML or what ever it is to show the p_name. So I would think (not an experet at ASP/HTML) that you need to alter one of the lines which shows the records data, maybe mail_from
Actually i just realized, it only writes the name of the user who is logged in. if i am logged in with the username 'Jeff', the id = 26 when i click on post.asp?id=26, it off course writes only the name Jeff.
i think this i because of the SQL-string: strSQL = "SELECT mails.*, profil.p_navn FROM mails INNER JOIN profil ON mails.mail_owner = profil.ID WHERE mails.mail_owner=" & Request.Querystring("id")
the Request.Querystring("id") = 26
So how do i change it, so it writes the proper name of the mail write?
There is actually a couple of things you have to think about here, one is the SQL and another is the ASP which moves the data around between the database and the ASP pages.
You say "it only writes the name of the user who is logged in" And "how do i change it, so it writes the proper name of the mail write"
So if we look at the fields in the dB, does "mails.mail_owner" contain the name of the mail writer? If so, is "Request.Querystring("id")" the ID of the mail writer or is it the ID of the person logged in?
Once we get these bits right then we can find out which fields we need to display from the SQL.
Tha mails.mail_owner is the id-number of the mail writer. so now i have made a mail.mail_owner_name. where the actually name is displayed. so it works now.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.