473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Quick question

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,syste m)
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-)
Sep 7 '06 #1
2 1337
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.

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,syste m)
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-)
Sep 9 '06 #2
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,syste m)
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-)
Sep 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.