473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select/Join query help (dropdown box?)

Hey everyone.

I'm re-writing a php Content Management System (based loosely on
phpNuke but specific to my site). My first major change was attempting
to normalise the Review section database structure. I've created a
Staff table with staff email addresses and names (plus id), and a
Reviews table with the text of the reviews. The Reviews table contains
an 'author' field with a value that corresponds to the Staff table.
When I display a review, I successfully used an Inner Join to link the
records and display the correct author name/email for the relevant
review.

My problem now is when editing the reviews. I originally had a basic
php page that simply selected the values and loaded them into a form,
but I can't get this to work now. Specifically, In the Submit form for
Reviews, I created a dropdown box which loaded the values from the
Staff table. When storing a review, it correctly stores the value for
the selected Author's ID in the Review table. My problem is when
editing the review, I cannot get php to lookup the author ID in the
Reviews table, then display in the dropdown the correct Author Name
from the Staff table for that specific review.

Could anyone help? The MySQL manual for joins may as well be written in
Greek.

Matt

Jul 17 '05 #1
17 2636
post your sql along with your error message.

Jul 17 '05 #2
Here's the SQL:

$query = "SELECT Band_Name, Record_Name, Review, Staff_Name, Staff_id,
Score, Cover_Image, Record_Label, Band_Site_URL
FROM review_test INNER JOIN staff ON Reviewer_Name = staff.Staff_id
WHERE id='$id'";
$result = mysql_query($qu ery) or die('Error : ' . mysql_error());

That's called in the code for the dropdown here:
echo "<select name='Reviewer_ Name' id='Reviewer_Na me'
value=\"$Staff_ id\">";
while($row2 = mysql_fetch_arr ay($result, MYSQL_NUM))
{
list($Staff_id, $Staff_Name) = $row2;
echo "<option value=\"$Staff_ id\">$Staff_Nam e</option>";
}
echo "</select>";

There's no error message, but currently that code is not loading the
correct value into the dropdown, it just loads the default one.

Jul 17 '05 #3
I noticed that Message-ID:
<11************ *********@z14g2 000cwz.googlegr oups.com> from
gu************@ gmail.com contained the following:
list($Staff_id, $Staff_Name) = $row2;


Your select query is fetching 9 columns but you're only listing two. If
it is returning a row at all. Have you tried print_r on $row2?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4
Geoff:

Sorry, I should have specified: As well as those two columns, there's
also some others being displayed.

This might make a little more sense:
http://www.scenepointblank.com/matt/...edit.php?id=26

This is the page I'm working with. I made some (bad) changes to the
code so currently the dropdown box is displaying the wrong value
(ordinarily it should be displaying a staff list) but basically, the
code you quoted is just for the dropdown. The other parts of the form
are supposed to be filled by the other 7 columns.

Also, I'm quite new to php/sql.. could you elaborate on what print_r
does?

Thanks again,
Matt

Jul 17 '05 #5
I noticed that Message-ID:
<11************ **********@o13g 2000cwo.googleg roups.com> from
gu************@ gmail.com contained the following:
This might make a little more sense:
http://www.scenepointblank.com/matt/...edit.php?id=26
Working perfectly. It's fetching the first two columns Band_Name,
Record_Name and putting them in the select box
This is the page I'm working with. I made some (bad) changes to the
code so currently the dropdown box is displaying the wrong value
(ordinarily it should be displaying a staff list) but basically, the
code you quoted is just for the dropdown. The other parts of the form
are supposed to be filled by the other 7 columns.
Whoa. If only one row is returned from this query, why do you need a
select box?
Also, I'm quite new to php/sql.. could you elaborate on what print_r
does?


It prints the contents of the array. It will show you what you've got.
Right now it looks like your Staff_Name will be contained in $row2[3]
and Staff_id in $row2[4]

So you could lose the list and do:

echo "<option value=\"".$row2[4]."\">".$row2[3]."</option>";

However, if you want all the reviewers in a drop down box, you'll have
to query the staff table separately and create an array from which you
can generate the drop down box. Each time round the loop check if the
value matches the value returned from the first query. If it is, print
selected in the option tag.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6
Geoff:
I need a select box just so we have the ability to edit this value
easily if neccessary. I guess it just makes it easier than typing in
the ID for each staff member, when we have around 30..

As for the code: I used the $row2[3] and $row2[4] values, they worked
great, displaying the $author value for the correct review. However,
when I uncommented a part of my code, they became blank. The code I
uncommented? That which loads the other values into the form.

For example:
http://www.scenepointblank.com/matt/...dit.php?id=240

If I comment out those lines, we get this:
http://www.scenepointblank.com/matt/...t_2.php?id=240

This is the code I'm commenting out:

$i=0;
while ($i < $num) {

/*$Band_Name=mys ql_result($matt result,$i,"Band _Name");
$Record_Name=my sql_result($mat tresult,$i,"Rec ord_Name");
$Review=mysql_r esult($mattresu lt,$i,"Review") ;
$Staff_Name=mys ql_result($matt result,$i,"Staf f_Name");
$Staff_id=mysql _result($mattre sult,$i,"Staff_ id");
$Score=mysql_re sult($mattresul t,$i,"Score");
$Cover_Image=my sql_result($mat tresult,$i,"Cov er_Image");
$Record_Label=m ysql_result($ma ttresult,$i,"Re cord_Label");
$Band_Site_URL= mysql_result($m attresult,$i,"B and_Site_URL");
*/

$i++;
}

Without that, none of the $variables load in the form. What am I doing
wrong?

Thanks for all your help so far.
Matt

Jul 17 '05 #7
I noticed that Message-ID:
<11************ *********@g47g2 000cwa.googlegr oups.com> from
gu************@ gmail.com contained the following:
Geoff:
I need a select box just so we have the ability to edit this value
easily if neccessary. I guess it just makes it easier than typing in
the ID for each staff member, when we have around 30..
Read my reply again. The query you are running seems to return one row
only. To get a drop down box of all staff members you'll have to run
/another/ query on the staff members table.
As for the code: I used the $row2[3] and $row2[4] values, they worked
great, displaying the $author value for the correct review. However,
when I uncommented a part of my code, they became blank. The code I
uncommented? That which loads the other values into the form.

For example:
http://www.scenepointblank.com/matt/...dit.php?id=240

If I comment out those lines, we get this:
http://www.scenepointblank.com/matt/...t_2.php?id=240

This is the code I'm commenting out:

$i=0;
Why do we have a loop here? It's still only one record isn't it?while ($i < $num) {

/*$Band_Name=mys ql_result($matt result,$i,"Band _Name");
$Record_Name=m ysql_result($ma ttresult,$i,"Re cord_Name");
$Review=mysql_ result($mattres ult,$i,"Review" );
$Staff_Name=my sql_result($mat tresult,$i,"Sta ff_Name");
$Staff_id=mysq l_result($mattr esult,$i,"Staff _id");
$Score=mysql_r esult($mattresu lt,$i,"Score");
$Cover_Image=m ysql_result($ma ttresult,$i,"Co ver_Image");
$Record_Label= mysql_result($m attresult,$i,"R ecord_Label");
$Band_Site_URL =mysql_result($ mattresult,$i," Band_Site_URL") ;
*/

$i++;
}

Without that, none of the $variables load in the form. Why do you think they would?
What am I doing
wrong?


It seems a very complicated way of doing it
$myrow=mysql_fe tch_array($resu lt) will fetch an associative array of
variables in the form $myrow['Band_Name'], $myrow['Record_Name'] and so
on. You can then use these to populate your form
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #8
Okay, I seem to have managed to do all of this, thanks a ton.
http://www.scenepointblank.com/matt/...view.php?id=99

See the extra dropdown at the end? That's a test I'm adding. It's
displaying all the staff from a seperate query, but I can't get it to
load the correct value.

You said "Each time round the loop check if the value matches the value
returned from the first query. If it is, print selected in the option
tag.".

My code:

$query2 = "SELECT Staff_id, Staff_Name FROM staff";
$result2 = mysql_query($qu ery2) or die('Error : ' . mysql_error());

...

echo "<select name=\"Reviewer _Name\" id=\"Reviewer_N ame\"
value=\"Staff_i d\">";
while($row2 = mysql_fetch_arr ay($result2, MYSQL_NUM))
{
list($Staff_id, $Staff_Name) = $row2;
echo "<option value=\"$Staff_ id\">$Staff_Nam e</option>";
}
echo "</select>";

How can I do what you suggested?

Just to clarify, I really appreciate this help. I'm only a few weeks
into php (does it show?!) and this is really helping me out.

Matt

Jul 17 '05 #9
I noticed that Message-ID:
<11************ **********@z14g 2000cwz.googleg roups.com> from
gu************@ gmail.com contained the following:
How can I do what you suggested?
Let's assume you have defined a variable $Staff_id_db from your first
query

while($row2 = mysql_fetch_arr ay($result2, MYSQL_NUM))
{
list($Staff_id, $Staff_Name) = $row2;
//check if this option should be selected
if($Staff_id==$ Staff_id_db){
$selected=" selected";
}
else{
$selected="";
}

echo "<option
value=\"$Staff_ id\"$selected>$ Staff_Name</option>";
}
Just to clarify, I really appreciate this help. I'm only a few weeks
into php (does it show?!) and this is really helping me out.


Cool. :-) (But I'm going to bed now...)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2290
by: jagg | last post by:
Hi, i save junior football results in a mysql table (FIELDS id jugend autor sptag ergebnis spdatum zeit ) With the following lines I generate a site whichs gives me ALL results Code: <table border="0" cellspacing="1" cellpadding="2" class="content" width="100%">
3
3548
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
4
2864
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table that links to a transaction items table that links to the products table: (User Table) UserID Other user data
3
6472
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
1
4178
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
4
10325
by: Ben | last post by:
I believe I am missunderstanding how subqueries work. I simple subquery works fine but when I wish do compare 2 or more fields at once I don't get the results I wish. Table A ID First Last Other 1 A Z 1 2 B Y 2 3 C Z 3
0
1917
by: Susan Bricker | last post by:
The following error: "The current field must match the join key '?' in the table that seves as t the 'one' side of one-to-many relationship. Enter a record in the 'one' side table with the desired key value, and then make the entry with the desired join key in the 'many-only' table." ... happens when I click on an entry of a combobox. HELP!! Here's the background:
0
2234
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability, architecture. Case 1: On a web page I would like to render a dropdown list
0
1284
kcdoell
by: kcdoell | last post by:
Hello: I have a form that I am using to help me display a query that I have built. On that form I have multiple combo boxes that I have called cobMonth, cobYear, cobWorkingRegion, etc... (basically these are certain field names in my query). I also have a command button called “Run query”. Currently, I have designed it so that if I select any of the options in my dropdown list I am able to narrow down my query result. I accomplished this by...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.