Re: combine implode() and array_keys() to get a string of key names

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Friday 07 July 2006 10:03, Janet Valade wrote:

> Actually, it doesn't matter what the indexes are when you use implode.
> Implode just puts the values into a string. Here's code that will work.
>
>    $field_array = array_keys($fields_form);
>    $fields = implode(",",$field_array);
>    $values = implode('","',$fields_form);
>    $query = "INSERT INTO Table1 ($fields) VALUES (\"$values\")";
>    $result = mysqli_query($cxn,$query);
>
> $fields_form is an associative array of all the fields to be entered
> into the database, with the field names as keys and the field values as
> values. This code quotes the values in the $values string in the query,
> as needed if the values are strings.

It is slightly more complicated than that, since if the value is numeric and 
going into a numeric field, then it's not supposed to be quoted.  (MySQL 
generally doesn't care, but some other databases may; I'm not certain.)  

I'm actually currently in the midst of getting this functionality added to the 
Drupal CMS.  Feel free to look at the patch I've submitted[1].  It has some 
Drupal-isms in it, but for the most part is fairly clear.  It also handles 
insert, update, and delete, too.

[1] http://drupal.org/node/30334

-- 
Larry Garfield			AIM: LOLG42
larry@xxxxxxxxxxxxxxxx		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux