Using the $_POST array (or any array) to populate a database

I was wondering if it is possible to use an array to populate a database to help save time. By that I meant doing the following…

instead of doing a query for the following

insert into table (option1, option2) Values (value1,value2)

I could simply have an array as following


then find a way to use that array to populate the query. This way I can have as many variables as I want and simply use the key for the array as the field name and the array content and the content for the database.

This will save a lot of time by simply having 1 class which I can use for all updates as well as inserts. I’m not a pro at PHP and I’m simply trying to learn and save time. Any help would be appreciated.

“INSERT INTO table (".implode(",",array_keys($_POST)).") VALUES ('”.implode(“‘,’”,array_values($_POST)).“');”

Note: Only works if all values are of non-numerical types. Otherwise it has to be slightly more complex.

Need to be careful with that, because it’s open to SQL Injection attacks

Use esacepshellcmd and mysql_real_escape_string. This will increase the security.