Good work peter, I loved your inclusion of step 5 in the functions so as one can apply functions to particularvalues before creating the sql string! 


_______________________________________________________________
its ok child, He's heard you
   

 


On Tue, Aug 9, 2011 at 1:41 PM, Peter Karunyu <pkarunyu@gmail.com> wrote:
Good people,
I have finally managed to write the aforementioned function. Below are the highlights.

Pros
1. The names of your form fields don't have to match the database column names, thanks to aliases
2. By only generating the column_name=$value part of the INSERT query, the function seeks to be database agnostic, sort of
3. You can specify which fields to ignore, e.g. the submit button and other hidden fields
4. The function is able to wrap string data with single quotes, and leave numerical data unwrapped, but only if u tell it so

Cons
1. The code is really long (but easy to read), can do with a bit of cleaning up and optimization
2. Maybe it would be better rewritten in OOP PHP
3. I have not run it thru xdebug, I bet that would reveal lots of interesting things
4. Currently, it can only save to one table
5. If you have a multi select form field, it contents will get converted into a string with a separator

TODO
1. Make it able to save to more than one table
2. Find a better way to deal with multiselect data

Credits
Skunks Ndungi, Chris Mwirigi, James Nzomo, Haggai Nyang for inspiration and code,
and of course the fellas at CodeIgniter for ideas :-)

Leteni feedback pap!



On Tue, Nov 9, 2010 at 10:32 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
@Chris, thanks. Let me get my hands dirty, I'll post what I have done laterz.



On Tue, Nov 9, 2010 at 10:04 AM, Chris Mwirigi <mwirigic@gmail.com> wrote:
in that case you could just get ur hands dirty and do the function urself.

if you ensure that your form elements match the table name, you can have a function to the quick one ave written below, it returns the last insert id

function update($_POST, $tableName)
{
$tableCols = "(";
$colValues = "(";
foreach($_POST as $key=>$value)
{
     $tableCols. = $key.",";
     $colValues = "'".mysql_real_escape_string($value)."',";
   

}
$tableCols .=")";
$colValues .=")";

$sql = "insert into $tableName $tableCols values $colValues";
$res = mysql_query($sql) or ("handle ur errors");
$num = mysql_insert_id();

return $num;

}


On Tue, Nov 9, 2010 at 9:49 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
Already looked into code igniter, it has something similar, I think. Zend framework has the exact same thing I need, but using a framework for the need I have at hand is like chartering the VMS Eve for a trip to shaggs :-)



On Tue, Nov 9, 2010 at 9:36 AM, Chris Mwirigi <mwirigic@gmail.com> wrote:
isn't it time you looked at a framework perhaps?

codeIgniter
cake
Yii
zend

./chris



On Tue, Nov 9, 2010 at 9:20 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
Good people,
I seek to write a generic, reusable set of functions that, given a GET/POST array, a table name and a few other instructions, can retrieve data from a form and save it in one or more tables. Anyone know of anything similar which I can build upon?

If there is one thing I find totally boring, its processing form data. Oh yeah, ideas on how to do this are also welcome, below are my initial ideas:

----
i
Parameters to be received by function:
  1. Associative array with name of form element and corresponding name of table column
  2. Name of table, to be used to construct the INSERT statement
  3. A flag to instruct the function to return the last_insert_id() for those cases where you need it for the next save

To be returned by the function
  1. The last_insert_id() if specified
  2. A boolean value indicating if the saving was successful or not

Anything else I am missing?

_______________________________________________
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke


_______________________________________________
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke


_______________________________________________
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke


_______________________________________________
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke




--
Regards,
Peter Karunyu
-------------------
Web Performance Evangelist
Make your website and web app faster.


_______________________________________________
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke