dynamic drop down that can perform queries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

dynamic drop down that can perform queries

Post by dsick »

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.
User avatar
jgadrow
Forum Newbie
Posts: 22
Joined: Wed Jun 17, 2009 7:56 pm
Location: Cincinnati, Ohio
Contact:

Re: dynamic drop down that can perform queries

Post by jgadrow »

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:

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'] . ');';
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. :)
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

Re: dynamic drop down that can perform queries

Post by dsick »

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 :banghead:

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?
Post Reply