0

I'm a beginner with basic PHP knowledge. I have a multiple drop down menu item list that queries my database. On submit I would like to show the description of the item selected from the database.

My query

$cat3=$_GET['cat3'];
if(isset($cat3) and strlen($cat3) > 0){
$quer3="SELECT DISTINCT subcat2, detail FROM subcategory2 where subcat_id=$cat3 order by subcat2"; 
}else{$quer3="SELECT DISTINCT subcat2 FROM subcategory2 order by subcat2"; }

What's being posted on sumbit

echo "<select name='subcat3' ><option value=''>Select one</option>";
foreach ($dbo->query($quer3) as $noticia) {
echo  "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";
}
echo "</select>";

Is it possible to pass my detail field along within subcat3 or do I have to create another table named details and another query ($quer4) and link it by the subcat2 id?

Any general information similar to an item search result(ebay) would help.

Frank
  • 3
  • 4
  • Anything inside the form being submitted will be passed in the ***GET*** request – adeneo Apr 14 '15 at 19:40
  • Well when $cat3 isset you already Select Distinct subcat2, and detail, so just call it like you did with `$noticia[subcat2]`. –  Apr 14 '15 at 19:41

1 Answers1

0

Please consider using placeholders too rather than passing the GET param directly into your SQL - There's a good post here that I recommend you taking a look at:

How can I prevent SQL injection in PHP?

But - this should work (but isn't ideal)

SELECT DISTINCT subcat2
     , detail
     , ( SELECT cat3_descr 
           FROM subcategory3 
          WHERE subcat_id = ?
       ) AS cat3_descr
  FROM subcategory2
 WHERE subcat_id = ?
ORDER BY subcat2 ASC

This isn't perfect though as it's having to do an inline sub-query - it'd be better to JOIN it on your subcategory2 table, but I imagine you'd run into some GROUPing problems with your DISTINCT subcat2 (assuming it's not unique - in which case, do you need to group subcat2 at all?)

Community
  • 1
  • 1
ash
  • 1,224
  • 3
  • 26
  • 46