Robert Builder
Robert Builder

Reputation: 57

php/sql JOIN two tables of data from $variable created from dropdown selection

I am a newbie, so new that I hope I can make this question clear enough.
Database has two tables;

tblnames > [clmid, clmnames]
tblpol > [clmDate, clmCategory, clmnames, clmComment]

My goal is: The user selects a category from a dropdown list, clicks a button and returns all rows of [tblpol.clmDate, tblnames.clmnames, tblpol.clmComment] data that match the user selection value $Category.

tblpol.clmnames contains only a names id so I don't want to display that, I want to display the full text name which is stored in tblnames.clmnames I think I need a JOIN to do this. Here is my attempt which doesn't work of course and I get the error Unknown column 'XYZ' in 'where clause'

 $clmCategory = $_GET['clmCategory'];
 $clmCategory = mysql_real_escape_string($clmCategory);
 $query = "SELECT tblpol.clmDate, tblnames.clmnames, tblpol.clmComment
           FROM tblpol
           INNER JOIN tblnames ON (tblpol.clmnames = tblnames.clmnames)
           WHERE clmCategory = $clmCategory";

Upvotes: 1

Views: 1056

Answers (1)

Andrew
Andrew

Reputation: 20071

You did ask the question well.

But just need to put quotation marks around the category variable, like so:

WHERE clmCategory = '$clmCategory'";

Also, you might need to join on the id rather than the name, like this:

INNER JOIN tblnames ON tblpol.id = tblnames.id

But I don't see your table structure so you might have to update the column names I used.

Upvotes: 5

Related Questions