pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Daniele Orlandi <daniele@orlandi.com>
To: pgsql-sql@postgresql.org
Subject: Re: [SQL] What do I need to escape in an Insert ?
Date: Fri, 31 Jul 1998 14:26:51 +0200
Message-ID: <35C1B80B.325F111D@orlandi.com> (raw)
References: <199807310501.BAA11175@candle.pha.pa.us>

Bruce Momjian wrote:
> 
> > I'm not sure if PHP's addslashes command will take care of single
> > quotes, I haven't tried it yet. I believe the proper escape for a
> > single quote in SQL is to put another single quote in front of it.
> > So if a person's last name is "O'Brien" it would be inserted as:
> >
> > INSERT INTO personel (fname,lname) VALUES ('Michael','O''Brien');
> >
> > So what I do is a Regular Expression Replace in PHP, like this:
> >
> >    $lname = ereg_replace("'", "''", $lname);
> 
> But this does not handle backslashes in the string.  I think the PHP
> function does both.

Looking at the source of the Bug Database at php.net, I noticed that strings are
inserted as they are, without any escaping. I still don't know where and why,
but PHP seems to escape quotes and slashes by itself when working with strings.

For example, I have the following code in my page:

$Query=sprintf("INSERT INTO bugdb (email,sdescr,ldescr,version,status) VALUES (
'%s','%s','%s','%s','Open' )",$EMail,$SDescr,$LDescr,$Version);

echo $Query;

In the textarea (ltext) I put:

\\
\n
'
"
abc

And the resulting query is:

INSERT INTO bugdb (email,sdescr,ldescr,version,status) VALUES (
'daniele@orlandi.com','Test3','\\\\ \\n \' \" abc','3.0.0','Open' )

Well, I've not yet been able to confuse PostgreSQL and everything i get from a
select is identical to what I put with the insert.

Bye!

-- 
 Daniele

-------------------------------------------------------------------------------
Why use Windows, since there is a door?
(By fachat@galileo.rhein-neckar.de, Andre Fachat)
-------------------------------------------------------------------------------
 Se telecom italia aggiungesse uno scatto al giorno ad ogni abbonato, dal
 primo Gennaio avrebbe rubato 671.633.000.000 Lire.
-------------------------------------------------------------------------------
 Daniele Orlandi - Utility Line Italia - http://www.orlandi.com
 Via Mezzera 29/A - 20030 - Seveso (MI) - Italy
-------------------------------------------------------------------------------



view thread (5+ messages)  latest in thread

Message-ID: <35C1B80B.325F111D@orlandi.com>
Permalink:  ../35C1B80B.325F111D@orlandi.com/
Also on:    postgresql.org/message-id/35C1B80B.325F111D@orlandi.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: daniele@orlandi.com
  Subject: Re: [SQL] What do I need to escape in an Insert ?
  In-Reply-To: <35C1B80B.325F111D@orlandi.com>
  References: <199807310501.BAA11175@candle.pha.pa.us> <35C1B80B.325F111D@orlandi.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox