From: "Mark Ackroyd" <[EMAIL PROTECTED]>

> I always found this way of inserting data into a database messy. Here is 
> a handy function to do array inserts and it builds the sql for you.
> 
> function arrayINSERT($a,$tablename)
> {
> $sql = "INSERT INTO $tablename (";
> foreach($a as $key => $value)
> {
> $sql .= $key .",";
> } 
> $sql[strlen($sql)-1] = ')';
> $sql .= " VALUES (";
> foreach($a as $key => $value)
> {
> if (gettype($value) == 'string')
> { 
> $sql .= "'". addslashes($value) ."',";
> }
> else
> {
> $sql .= $value .",";
> }
> }
> $sql[strlen($sql)-1] = ')';
> return $sql;
> }

To get your list of columns, you could do this:

$column_list = implode(',',array_keys($a));
$sql = "INSERT INTO $tablename ($column_list) VALUES ";

That way you only have to loop through $a once. 

---John Holmes...

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

Reply via email to