473,387 Members | 3,750 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,387 software developers and data experts.

PHP and quote

D1R
I have to migrate a web application written in PHP and I am using
PHP 5.2.6 ISAPI.

The problem I have is related to some sql queries that have the
following format:

$sql = "insert into table_name values ('','" . $value1 . "','" .
$value2 . "',')";

This fails because PHP will translate this in something like:

Insert into table_name values (", '10','15')
In other words if I have in my script something like '10' that will
translated into
the same '10' but something like '' will make the final query string
to look different
and to fail.

This script run without problems in the old server and I guess should
be something
in PHP.ini but I cannot find what is different.

Is there a setting in php.ini that can fix this?

Thank you!

Aug 21 '08 #1
9 1236
On 21 Aug, 22:43, D1R <dlr...@gmail.comwrote:
I have to migrate a web application written in PHP and I am using
PHP 5.2.6 ISAPI.

The problem I have is related to some sql queries that have the
following format:

$sql = "insert into table_name values ('','" . $value1 . "','" .
$value2 . "',')";

This fails because PHP will translate this in something like:

Insert into table_name values (", '10','15')
In other words if I have in my script something like '10' that will
translated into
the same '10' but something like '' will make the final query string
to look different
and to fail.

This script run without problems in the old server and I guess should
be something
in PHP.ini but I cannot find what is different.
You've totally failed to isolate the problem but offerd various bits
of irrelevant information - the example you've provided gives no
indication of where the data might have come from, and the fact that
you are creating an SQL query (badly) has nothing to do with the
problem. Taking a random stab in the dark, it looks like a scope
problem - your old server may have had register_globals on but your
new one doesn't (which would also indicate that your code is badly
structured).

Read this:
http://www.catb.org/~esr/faqs/smart-questions.html

and this:
http://www.php.net/register_globals

and this:
http://en.wikipedia.org/wiki/Structured_programming

C.
C.
Aug 21 '08 #2

"D1R" <dl****@gmail.comwrote in message
news:cd**********************************@b1g2000h sg.googlegroups.com...
>I have to migrate a web application written in PHP and I am using
PHP 5.2.6 ISAPI.

The problem I have is related to some sql queries that have the
following format:

$sql = "insert into table_name values ('','" . $value1 . "','" .
$value2 . "',')";

This fails because PHP will translate this in something like:

Insert into table_name values (", '10','15')
that's a false conclusion. the " in the line above is really two tics...not
a single quote. that's the first misleading premise.

second, i can only say that this is more likely to be based on the database
you are using. in oracle, '' is the same thing as NULL. in mysql, ms sql
server, et. al. '' is merely an empty string. so, if you're using oracle and
the first field in table_name is set as NOT NULL, then your insert will
fail...because of the db, not php. there could also be constraints and
triggers put on the table that won't allow you to insert NULL *or* a blank
string into the first field.

it's hard to tell you anything unless you post the actual error. and, i
don't like assuming anything.

cheers.
Aug 21 '08 #3
D1R


Thank you for your fast answer.

I use to write code in PHP but I am not an expert so I did not
provide enough information. Sorry about that.

The code was written by somebody else and it is using MySQL as DB.
I had to migrate the web server and that was the time when I start
having problems and I discover that some options did not work.
The code I was talking about is:
$SQL = "Insert into tablename values('','".$_POST['name']."','Y')";

I tried to troubleshoot this and I used echo to display the query and
I found that
the result was something like

"Insert into tablename values("'Name','Y')
I replaced that line with

$SQL = "Insert into tablename values(NULL,'".
$_POST['name']."','Y')
And that solved the problem but I believe that should be another
way to do this and I guess that something should be in PHP.ini.
Thank you again.


On Aug 21, 5:58*pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
On 21 Aug, 22:43, D1R <dlr...@gmail.comwrote:


I have to migrate a web application written in PHP and I am using
PHP 5.2.6 ISAPI.
The problem I have is related to some sql queries that have the
following format:
$sql = "insert into table_name values ('','" . $value1 . "','" .
$value2 . "',')";
This fails because PHP will translate this in something like:
Insert into table_name values (", '10','15')
In other words if I have in my script something like '10' that will
translated into
the same '10' but something like '' will make the final query string
to look different
and to fail.
This script run without problems in the old server and I guess should
be something
in PHP.ini but I cannot find what is different.

You've totally failed to isolate the problem but offerd various bits
of irrelevant information - the example you've provided gives no
indication of where the data might have come from, and the fact that
you are creating an SQL query (badly) has nothing to do with the
problem. Taking a random stab in the dark, it looks like a scope
problem - your old server may have had register_globals on but your
new one doesn't (which would also indicate that your code is badly
structured).

Read this:http://www.catb.org/~esr/faqs/smart-questions.html

and this:http://www.php.net/register_globals

and this:http://en.wikipedia.org/wiki/Structured_programming

C.
C.- Hide quoted text -

- Show quoted text -
Aug 21 '08 #4
D1R wrote:
>
Thank you for your fast answer.

I use to write code in PHP but I am not an expert so I did not
provide enough information. Sorry about that.

The code was written by somebody else and it is using MySQL as DB.
I had to migrate the web server and that was the time when I start
having problems and I discover that some options did not work.
The code I was talking about is:
$SQL = "Insert into tablename values('','".$_POST['name']."','Y')";

I tried to troubleshoot this and I used echo to display the query and
I found that
the result was something like

"Insert into tablename values("'Name','Y')
I replaced that line with

$SQL = "Insert into tablename values(NULL,'".
$_POST['name']."','Y')
And that solved the problem but I believe that should be another
way to do this and I guess that something should be in PHP.ini.
Thank you again.


On Aug 21, 5:58 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
>On 21 Aug, 22:43, D1R <dlr...@gmail.comwrote:


>>I have to migrate a web application written in PHP and I am using
PHP 5.2.6 ISAPI.
The problem I have is related to some sql queries that have the
following format:
$sql = "insert into table_name values ('','" . $value1 . "','" .
$value2 . "',')";
This fails because PHP will translate this in something like:
Insert into table_name values (", '10','15')
In other words if I have in my script something like '10' that will
translated into
the same '10' but something like '' will make the final query string
to look different
and to fail.
This script run without problems in the old server and I guess should
be something
in PHP.ini but I cannot find what is different.
You've totally failed to isolate the problem but offerd various bits
of irrelevant information - the example you've provided gives no
indication of where the data might have come from, and the fact that
you are creating an SQL query (badly) has nothing to do with the
problem. Taking a random stab in the dark, it looks like a scope
problem - your old server may have had register_globals on but your
new one doesn't (which would also indicate that your code is badly
structured).

Read this:http://www.catb.org/~esr/faqs/smart-questions.html

and this:http://www.php.net/register_globals

and this:http://en.wikipedia.org/wiki/Structured_programming

C.
C.- Hide quoted text -

- Show quoted text -

No, there is nothing in the php.ini file to change sql statements. All
they are are strings to PHP.

However, if you switched to a new server, chances are your MySQL version
changed also - and that's where your problem is. Try comp.databases.mysql.

Hint: When you're talking about SQL statements, chances are your best
place to start would be the database newsgroup, not here.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 22 '08 #5
D1R
However, if you switched to a new server, chances are your MySQL version
changed also - and that's where your problem is. *Try comp.databases.mysql.

Hint: When you're talking about SQL statements, chances are your best
place to start would be the database newsgroup, not here.
------------------------------------

Jerry,

It is not about SQL statements it is about the fact that
if you'll do something like:

$SQL = "Insert into tablename values('','".
$_POST['name']."','Y')";
echo $SQL;

You'll get:

Insert into tablename values("'Name','Y')

while what I expect to have is:

Insert into tablename values('','Name','Y')

On the other hand

$SQL = "Insert into tablename values('1','".
$_POST['name']."','Y')";
echo $SQL;

will display

Insert into tablename values('1','Name','Y')
This worked fine in the old server.
This is the reason why I believe should be a setting in PHP.ini
but I cannot figure out what is that.

Thank you.
Aug 22 '08 #6
D1R wrote:
>However, if you switched to a new server, chances are your MySQL version
changed also - and that's where your problem is. Try comp.databases.mysql.

Hint: When you're talking about SQL statements, chances are your best
place to start would be the database newsgroup, not here.

------------------------------------

Jerry,

It is not about SQL statements it is about the fact that
if you'll do something like:

$SQL = "Insert into tablename values('','".
$_POST['name']."','Y')";
echo $SQL;

You'll get:

Insert into tablename values("'Name','Y')

while what I expect to have is:

Insert into tablename values('','Name','Y')

On the other hand

$SQL = "Insert into tablename values('1','".
$_POST['name']."','Y')";
echo $SQL;

will display

Insert into tablename values('1','Name','Y')
This worked fine in the old server.
This is the reason why I believe should be a setting in PHP.ini
but I cannot figure out what is that.

Thank you.
PHP does not substitute " for ''. One of three things has occurred:

1. You put " in there
2, You read the '' incorrectly.
3. You are not executing the code you think you are executing.

There is nothing special about SQL - it's just a string to PHP. And if
what you said actually occurred, millions of servers all over the world
would be crashing.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 22 '08 #7
JC
"D1R" <dl****@gmail.comwrote...
: It is not about SQL statements it is about the fact that if you'll
: do something like:
:
: $SQL = "Insert into tablename values('','". $_POST['name']."','Y')";
: echo $SQL;
:
: You'll get:
:
: Insert into tablename values("'Name','Y')
:
: while what I expect to have is:
:
: Insert into tablename values('','Name','Y')

Either you are broken, your fingers are broken or your logic is
mistaken. :-)

First, I would not use variable names that could possibly be reserved
words. Yeah, I know that the dollar-symbol means it's a variable. The
point is to NOT ever define things like $SQL. But that's just me and a
whole lot of others that feel that way.

Your logic above fails to work though. You stated that PHP writes out:

: Insert into tablename values("'Name','Y')

However, note, you probably mistyped that. The terminating " fails to
appear.

Don't know if this will work, but you might also try prefixing the '
with a backslash. Check the extensions that affect string variables
(and/or security if such exist).

Also, take NOTE, that your string above, which uses the $_POST variable
is subject to SQL injection.

Try configuring the php.ini file with E_ALL to report all errors/notices.

Hope some of this helps.

--
JC
You Have More Than Five Senses
http://www.associatedcontent.com/art...ve_senses.html
Aug 22 '08 #8
First of all, why not declaring the table fields you want to fill in?

$sql="INSERT INTO tablename('name','value1','value2') VALUES
('$name','$value','$value2')";

So you won't need to insert Empty values (wich makes no sence to me, if
you declared a standard value in the Database).
Second thing:

D1R wrote:
>
$SQL = "Insert into tablename values('','".
$_POST['name']."','Y')";
echo $SQL;

You'll get:

Insert into tablename values("'Name','Y')

while what I expect to have is:

Insert into tablename values('','Name','Y')

*NEVER, NEVER* directly use the data sent by the user. This Data *has* to
be seen as Evil. This because of SQL Injections¹.
Write a function to avoid people altering or removing your Database
Contents. If you are not able to, I can help you.


¹: http://en.wikipedia.org/wiki/SQL_injection
Aug 22 '08 #9
D1R wrote:
>However, if you switched to a new server, chances are your MySQL version
changed also - and that's where your problem is. Try comp.databases.mysql.

Hint: When you're talking about SQL statements, chances are your best
place to start would be the database newsgroup, not here.

------------------------------------

Jerry,

It is not about SQL statements it is about the fact that
if you'll do something like:

$SQL = "Insert into tablename values('','".
$_POST['name']."','Y')";
echo $SQL;

You'll get:

Insert into tablename values("'Name','Y')

while what I expect to have is:

Insert into tablename values('','Name','Y')

On the other hand

$SQL = "Insert into tablename values('1','".
$_POST['name']."','Y')";
echo $SQL;

will display

Insert into tablename values('1','Name','Y')
This worked fine in the old server.
This is the reason why I believe should be a setting in PHP.ini
but I cannot figure out what is that.

Thank you.
You don't appear to be escaping the data in $_POST['name']. You can
use mysql_real_escape_string or prepared statements if you're using
PDO or mysqli. Unless you escape the data, user data can easily be
crafted to either break the query, or inject unintended code (SQL
injection), which may be the cause of your problem.

Also, as Marco Lussi mentioned, specifying the field names is a lot
easier, as well as clearer. Check comp.databases.mysql for specifics
on the SQL itself.

--
Curtis (http://dyersweb.com)
Aug 23 '08 #10

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

Similar topics

1
by: jmar | last post by:
I posted on this topic a while back and received some good responses. However, I have better insight into what I'm looking to do so I am tapping the wealth of experience here again hoping to find a...
3
by: nyenyec | last post by:
urllib.quote chokes on unicode in 2.4.4. 2.4.4 (#1, Oct 18 2006, 10:34:39) Traceback (most recent call last): File "<stdin>", line 1, in ? File...
4
by: wesbland | last post by:
>From my understanding, when a string is stored in VB.NET and you look at it in the debugger, it has a quote on both sides to signify that it is a string as opposed to a char or int or whatever. ...
8
by: plemon | last post by:
alright here is what it is: a computer site where customers build custom system by selecting from forms and radio buttons. what is needed: a database (MySQL) with php basicly... 1 users...
4
by: mtugnoli | last post by:
This is my XML file <?xml version="1.0" encoding="utf-8"?> <index> <folder Name="Test1"> <folder Name="Test2"> <files> <file Name="After You've Gone.mp3"/> <file Name="Prova.mp3"/> </files>
10
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
How can I use a quote as a literal so it does get confused as not a literal? Thanks! Bob
3
by: Rajesh | last post by:
Have you guys wanted to have stock tickers on ur desktop/ web pages, but can't use applets as they were heavy, too much configuration than here is one approach and light weight solution all for...
4
by: wxPythoner | last post by:
There's a thing that bugs me in Python. Look at this... SyntaxError: EOL while scanning single-quoted string Please focus on the part of the error message that states "while scanning...
3
by: rajmohan.h | last post by:
Hi all, Suppose I have a string which contains quotes inside quotes - single and double quotes interchangeably - s = "a1' b1 " c1' d1 ' c2" b2 'a2" I need to start at b1 and end at b2 - i.e. I...
9
by: WebArchitect | last post by:
I have a text file. My text file contains lines with double quotes around it. I trying to code if a line has double quotes around it Then make it bold My code is below and it is not working....
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.