Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 02:07 Der 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
           
i = 1
Do

    Response.Write "<tr>"
    Response.Write "<td width='1%'>&nbsp;</td>"
    Response.Write "<td width='2%'>&nbsp;</td>"   
    Response.Write "<td width='2%'><font face='Verdana' size='1' color='#FFFFFF'>" & rs("mail_from") & "</td>"
    Response.Write "<td width='19%'>&nbsp;</td>"
    Response.Write "<td width='55%'><font face='Verdana' size='1' color='#FFFFFF'>" & rs("mail_subject") & "</td>"
    Response.Write "<td width='15%'><font face='Verdana' size='1' color='#FFFFFF'>" & rs("mail_sendt") & "</td>"   
    Response.Write "<td width='5%'>&nbsp;</td>"
    Response.Write "<td width='1%'>&nbsp;</td>"         
    Response.write "</tr>"


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'
Avatar billede terry Ekspert
16. februar 2008 - 09:33 #1
SELECT mails.*, profiler.Navn
FROM mails INNER JOIN profiler ON mails.ID = profiler.ID;
Avatar billede terry Ekspert
16. februar 2008 - 09:35 #2
strSQL = "SELECT mails.*, profiler.Navn
FROM mails INNER JOIN profiler ON mails.ID = profiler.ID Where mail_owner = " & Request.Querystring("id")
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 15:02 #3
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
Avatar billede terry Ekspert
16. februar 2008 - 15:20 #4
strSQL = "SELECT mails.*, profiler.p_navn
FROM mails INNER JOIN profiler ON mails.mail_id = profiler.id Where mail_owner = " & Request.Querystring("id")
Avatar billede terry Ekspert
16. februar 2008 - 15:23 #5
another idea is to create a query in Access which includes both tables and then in your sql you would use (EXAMPLE)


strSQL = "SELECT * FROM YourQuery Where mail_owner = " & Request.Querystring("id")

where YourQuery is the name of the query in Access
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 15:27 #6
nu får jeg:


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
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 15:28 #7
ups.. får denne:

Microsoft OLE DB Provider for ODBC Drivers error '80040e10'

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

/k2j/jefjor/brock2/post.asp, line 18
Avatar billede terry Ekspert
16. februar 2008 - 15:30 #8
is it possible for you to send me your dB?
ekspertenATsanthell.dk
AT = @
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 15:31 #9
Sure, 2 sec
Avatar billede terry Ekspert
16. februar 2008 - 15:32 #10
which two field are related in the two tables?
Avatar billede terry Ekspert
16. februar 2008 - 15:34 #11
Maybe?

"SELECT mails.*, profiler.p_navn
FROM mails INNER JOIN profiler ON mails.mail_owner = profiler.id Where mail_owner = " & Request.Querystring("id")
Avatar billede terry Ekspert
16. februar 2008 - 15:41 #12
Hi Jeff
Can you resend after renaming the dB, Outlook is blocking the MDB file. I’m off out for around an hour and will look as soon as I get back.
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 15:46 #13
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
Avatar billede terry Ekspert
16. februar 2008 - 16:32 #14
Outlook blocks .mdb files so can you rename to .txt or something please?
Avatar billede terry Ekspert
16. februar 2008 - 16:36 #15
16/02-2008 15:46:06 so at least we dont get an error now?
Avatar billede terry Ekspert
16. februar 2008 - 16:57 #16
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?
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 17:14 #17
OK, i will send it as a word document, to your mail.. 2 sec
Avatar billede terry Ekspert
16. februar 2008 - 17:47 #18
with the information yu sent me this works

SELECT mails.*, profiler.p_navn
FROM mails INNER JOIN profiler ON mails.mail_owner = profiler.ID
WHERE mails.mail_owner=26


Which is almost the same as the SQL I gave 16/02-2008 15:34:10

Can you try it EXACTLY as it is so that you use 26 instead of Request.Querystring("id")?
Avatar billede terry Ekspert
16. februar 2008 - 17:49 #19
are you sure the original SQL worked, if it did then the new SQL I gave should also work.
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 17:54 #20
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
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 17:57 #21
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)
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 17:58 #22
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")
Avatar billede terry Ekspert
16. februar 2008 - 18:04 #23
I mean the SQL YOU gave in your original question, does that work?
Avatar billede terry Ekspert
16. februar 2008 - 18:06 #24
If the SQL I gave 16/02-2008 17:47:56 works then this should work

strSQL = "SELECT mails.*, profiler.p_navn
FROM mails INNER JOIN profiler ON mails.mail_owner = profiler.ID
WHERE mails.mail_owner=" & Request.Querystring("id")

If it doesnt work then I dont understamd how your original SQL does
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 18:07 #25
Yes this one?:
strSQL = "Select * From mails Where mail_owner = " & Request.Querystring("id")

it works fine
Avatar billede terry Ekspert
16. februar 2008 - 18:10 #26
The SQL I gave 16/02-2008 18:06:55 should work too then.

I'm off out, I'll look again tomorrow.
Avatar billede gyldenbrand Nybegynder
16. februar 2008 - 18:12 #27
Yes it works to :)

but it still writes the 'id' and not the 'p_name'
Avatar billede terry Ekspert
17. februar 2008 - 09:38 #28
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

Response.Write "<td width='2%'><font face='Verdana' size='1' color='#FFFFFF'>" & rs("mail_from") & "</td>"

To p_name

Response.Write "<td width='2%'><font face='Verdana' size='1' color='#FFFFFF'>" & rs("p_Name") & "</td>"
Avatar billede terry Ekspert
19. februar 2008 - 14:31 #29
does this help Jeff?
Avatar billede gyldenbrand Nybegynder
21. februar 2008 - 11:34 #30
Sorry, haven't been home the last few days. i will try the code later, and give you a feedback, thx.
Avatar billede gyldenbrand Nybegynder
21. februar 2008 - 21:58 #31
Thank you! it works perfectly :)
Avatar billede gyldenbrand Nybegynder
21. februar 2008 - 22:07 #32
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?
Avatar billede terry Ekspert
22. februar 2008 - 17:10 #33
Hi Jeff
Am I right in saying that you didnt make the code originally?
Avatar billede gyldenbrand Nybegynder
23. februar 2008 - 19:41 #34
No, i did write the code myself - i'm just new in database programming
Avatar billede terry Ekspert
24. februar 2008 - 09:20 #35
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.




So I assume that
Avatar billede gyldenbrand Nybegynder
25. februar 2008 - 11:17 #36
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.
Avatar billede terry Ekspert
28. februar 2008 - 20:01 #37
super
Avatar billede Ny bruger Nybegynder

Din løsning...

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.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester