I have a drop down menu and I would like to use the selection in a query.
Say I have the following, this is actually loaded from a db on my site but to limit the amount of code I post:
<select id="parent" name="parent">
<option value="P1">parent 1</option>
<option value="P2">parent 2</option>
</select>
I want to query the child table for P1 or P2 depending on what was selected so I have the following query:
$selected_parent=$_REQUEST['parent'];<-- Not sure if this is right at all
$query = "SELECT parent.*, child.*
FROM parent
INNER JOIN child
ON parent.value = child.parent
WHEN parent.value = '$selected_parent'";
I would have the two following tables:
PARENT CHILD
ID NAME VALUE ID NAME PARENT
01---parent1--P1 01----child1--P1
02---parent2--P2 02----child2--P1
03---parent3--P2 03----child3--P2
I know this is open to SQL Injection, I will fix it later, more curious as to how the variable working within a query
EDIT: Currently I get no results when using ‘$selected_parent.’
Thanks in advance
You’re extremely close, the only change I can see would be to wrap the dropdown box in a form tag, such as:
And then when you’re getting the variable, use $_POST instead of $_REQUEST:
Other than that your query should work just fine.