Reply to Re: Quick question

Your name:

Reply:


Posted by IchBin on 09/10/06 02:25

> IchBin wrote:
>> I am trying to do a SQL table insert. OK, no big deal. Well I am having
>> a problem with this code. I build a data structure $fields_values and
>> pass it to a function insertIntoDB() to issue the insert.
>>
>> The problem is in the actual INSERT Statement. The values for the column
>> data do not have "" around the Col values. So SQL flags the fields as
>> wrong. Example:
>>
>> INSERT INTO `Log` (ipAddress,action,groupPageName,namePageName,system)
>> VALUES (127.0.0.1,browse,Main,HomePage,Testing new code)
>>
>> Can some one point me in the right direction on wrapping " around the
>> data values so it will insert into the table.
>>
>> $table = 'myTable';
>>
>> $fields_values = array
>> (
>> 'ipAddress' => $_SERVER['REMOTE_ADDR'],
>> 'action' => $action,
>> 'groupPageName' => FmtPageName('$Group', $pagename),
>> 'namePageName' => FmtPageName('$Name', $pagename);,
>> 'system' => $system
>> );
>>
>> insertIntoDB($strTableName, $fields_values);
>>
>> function insertIntoDB($table, $fields_values)
>> {
>> $fields = implode(array_keys($fields_values), ',');
>> $values = implode(array_values($fields_values), ',');
>> $sqlStatement = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
>> ('.$values.')';
>> $res = mysql_query($sqlStatement)OR die(mysql_error());
>> return true;
>> }
>>
>> --
>> Thanks in Advance...
>> IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
>> 'If there is one, Knowledge is the "Fountain of Youth"'
>> -William E. Taylor, Regular Guy (1952-)
>

Joshie Surber wrote:
> In your insertIntoDB function, change:
> $values = implode(array_values($fields_values), ',');
> to this:
> $values = implode(array_values($fields_values), '","');
> and this:
> $sqlStatement = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
> ('.$values.')';
> to this:
> $sqlStatement = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
> ("'.$values.'")';
> This should quote everything. I don't remember if quoting non-string
> values matters, if so you would need to loop through $values and append
> quotes if is_string($value[n]) instead.
>

[Corrected message order in this thread]

Thanks Joshie the following resolved the problem:

$values="'".implode(array_values($fields_values),"','")."'";

--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация