dynamic drop down that can perform queries
Moderator: General Moderators
dynamic drop down that can perform queries
im trying to figure out a way to code a drop down menu that can perform queries... i want to insert based on what column name is selected from the drop down menu.. it would be nice if i didn't have to define a variable for each option because there could be like 20 columns i could submit to and i would have to test if each one of them is selected, then make the insert from the if and else ifs.. im beginning to realize that will be messy coding if i do it that way.
Re: dynamic drop down that can perform queries
I would code an array containing the options present in the drop-down. You always want to validate your dropdowns because your script can't tell if the form was submitted from a drop-down element or just a plain text field.
So, something like:
Obviouslly, you'd want to sanitize your data with whatever method is appropriate for your system. Please don't just insert the raw $_POST data until you've validated it. 
So, something like:
Code: Select all
$options = array
(
'value1' => 'column1',
'value2' => 'column2',
'value3' => 'column3',
);
if (!isset ($options [$_POST ['optionForm']]))
{
throw new ErrorException ('Invalid entry recorded for drop down!', E_ERROR);
}
$query = 'insert into tableName (' . $options [$_POST ['optionForm']] . ') VALUES (' . $_POST ['optionForm2'] . ');';Re: dynamic drop down that can perform queries
i will just call the sanitize method i wrote for other applications... im going to try this code and see if it works.. i have a list of subjects on my navigation... and i want to use the drop down to insert content in the selected option..
edit.. im looking at your code and i think i explaned this wrong..
i want the table names to be in the drop down list
and im going to be inserting collumns based on the table name selected from the drop down
you got the code right its just backwards due to my explanation
the table name would be present in the options, the table name should be the selected option
so to make my explanation more clear i want to know how to get the selected option... the query syntax will go as normal besides the INSERT INTO tablename, and table name will be the selected option.... will $_POST['selected'] work?
edit.. im looking at your code and i think i explaned this wrong..
i want the table names to be in the drop down list
and im going to be inserting collumns based on the table name selected from the drop down
you got the code right its just backwards due to my explanation
the table name would be present in the options, the table name should be the selected option
so to make my explanation more clear i want to know how to get the selected option... the query syntax will go as normal besides the INSERT INTO tablename, and table name will be the selected option.... will $_POST['selected'] work?