473,507 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick two Code questions..

Sorry I am new to PHP. First question, I hate to ask this but is there
an or statement in PHP that I can use in an IF clause? I can't seem to
find the format. Like say '|' or 'or'.

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($table, $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 1153
IchBin wrote:
Sorry I am new to PHP. First question, I hate to ask this but is there
an or statement in PHP that I can use in an IF clause? I can't seem to
find the format. Like say '|' or 'or'.

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($table, $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;
}
Logical or is copied from C et al. It is '||'. '|' is bit wise or of two
integers. Reference
http://php.benscom.com/manual/en/lan...rs.logical.php

The statement INSERT should look like the following:

INSERT INTO `Log` (ipAddress,action,groupPageName,namePageName,syste m)
VALUES ('127.0.0.1','browse','Main','HomePage','Testing new code')

If, as it appears, all your db columns are either CHAR, VARCHAR, or DATE
then this is easily done by:

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

If they are of mixed numeric and the aforementioned types, then your
fields_values assignment statement must be modified as follows for each
CHAR etc. column:

'action' ="'$action'",
Sep 7 '06 #2
Bob Stearns wrote:
IchBin wrote:
>Sorry I am new to PHP. First question, I hate to ask this but is there
an or statement in PHP that I can use in an IF clause? I can't seem to
find the format. Like say '|' or 'or'.

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($table, $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;
}
Logical or is copied from C et al. It is '||'. '|' is bit wise or of two
integers. Reference
http://php.benscom.com/manual/en/lan...rs.logical.php

The statement INSERT should look like the following:

INSERT INTO `Log` (ipAddress,action,groupPageName,namePageName,syste m)
VALUES ('127.0.0.1','browse','Main','HomePage','Testing new code')

If, as it appears, all your db columns are either CHAR, VARCHAR, or DATE
then this is easily done by:

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

If they are of mixed numeric and the aforementioned types, then your
fields_values assignment statement must be modified as follows for each
CHAR etc. column:

'action' ="'$action'",
Thanks you Bob for your help, all works now.

--
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 #3

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

Similar topics

7
2064
by: Elaine Jackson | last post by:
Two quick newbie questions: 1) Does Python have passing-by-reference? 2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the Python "copy" module (if I understand...
1
1524
by: YT | last post by:
Couple of quick ASP (3.0) Cookie questions: 1/ I'm using a cookie in my asp script to place cookies (within a key) so my code looks like: Response.Cookies( "quoteform" )( "name" ) = Session(...
3
2194
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
2
4885
by: Daren Hawes | last post by:
Hi I need a quick code example how to get the Session ID from an ASP.NET page in VB. Thx *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get...
4
1207
by: ryan_s | last post by:
1) My coding experience has all been with Visual Basic 6 (what I learned in school). Now with the whole ".NET" products, can I code using the same syntax (provided that I don't need to use the...
3
2110
by: Bart Van Hemelen | last post by:
I'm working on a project where the user of a site will receive custom content, depending on a set of parameters. The content will all be contained in UserControls (.ascx), that will be used as...
4
2258
by: Thiengineer | last post by:
Given the following code in C: char *s = {"program","test","load","frame","stack",NULL}; char **p = s + 2; Questions:
4
1267
by: Phil Latio | last post by:
Firstly, is the following code allowed assuming the addContent function expects as single argument? $page->addContent($form = new Form()); Secondly, I've been reading a tutorial and noticed the...
4
2247
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
0
7223
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
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7376
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...
1
7031
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
7485
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.