Cditty wrote:
A co-worker is teaching me to move to the next level in php. I have
started using associative arrays for my scripts, but I am having a problem
using them to do an insert into MySQL. Can someone give me an example of
how to do an insert to the database using these arrays? My array is
this...$item['itemID']
If you want to go up to even another level:

<?
$x['name'] = "Mike's";
$x['phone'] = 'fsdlfksdf';
echo sql($x);

function sql($a) {
 $k = implode(",",array_keys($a));
 array_walk($a,'slashadd');
 $v = "'".implode("','",$a)."'";
 return "insert into ($k) values ($v)";
}
function slashadd(&$bar) { $bar = addslashes($bar); }
?>

Now all you need to do is pass more info to the $x
array, and it'll make the insert SQL for you,
including adding the slashes, and you don't need to
worry about { } or where to put quotes and backslashes
and all that crap.  1 column or 50 columns
is absolutely no more work for you.

Modify it just slightly some by using REPLACE with
MySQL and you've got updates and inserts taken care
of.

PLUG: That's the sort of thing we get up to during our
PHP Training Courses.  More info at http://tapinternet.com/php


Michael Kimsal
http://phpappserver.com
http://www.phphelpdesk.com
734-480-9961


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to