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!
I've been trying to write a function that inserts data to a mysql db. I want it completely automated so it can be used with multiple db's and even use it on other sites i design. I've got it to grab the fieldnames from the table and format them for sql syntax, when echoed they display properly to the page. however when i change the echo to a return it only returns the index fieldname. Same thing with the values function the code is as follows:
I would not recommend nesting functions unless you have a really good reason to do so. You should verify that your functions actually return something valid and that you want. The function cleanValues() is too clever for its own good. Simple concatenation would be fine. And mysql_error() may help you find out if the queries are even working.
You are to be congratulated for getting as far as you have without the benefit of formal instruction, but the risk you take is getting too far out on a limb before realizing that it's going to break. I agree with arborint that nesting functions is not a good structure, at least for 99.99% of programming.
I think you may be overreaching for reusability. This would be better suited to OOP (Object Oriented PHP). You may want to read up on PHP classes.
But hang in there and show us any further ideas you have and I'm sure you will receive helpful analyses of your work.
The point in using field names (or column names) is to make sure the data goes into the right column. When you fetch the field names from the database like you do in your function, there is no mapping between field names and the values in $value_arr. The lazy approach would be to simply skip the field names in the query and just focus on the data inside VALUES(). But if you want to build a stable solution you have to map field names to data before you build the query. My suggestion is that you continue to improve on your functions (but avoid nested ones) and at the same time look into some existing Database Abstraction Layers.