Web Design Forums
Web Design Forums Forums Register Why Register? About WDF FAQ Members Feedback WDF Store
Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » quotes and apostrophes RSS
quotes and apostrophes
This thread was started by jbagley and has been viewed 502 times, and contains 4 replies, with the last reply made by jbagley.
Post Reply
1
44 points at 100%
jbagley, "Must...post...more...!" Home page   Private message  
Posted February 23 '05 at 06:14 AM
      Posts: 845
What is the best way of handling them when it comes a Database driven application?

I know the methods used are stripslashes and addslashes, but what is best practice? Do you add the slashes before you insert into the database and then strip them before you print it out to the screen?

Is there a better or faster way of doing it?

Advertisement Register for free to hide these ads and participate in discussions!

2
312 points at 100% Moderator Repute WDFplus Member
Posted February 23 '05 at 07:14 AM
      Posts: 1,952
Do you add the slashes before you insert into the database and then strip them before you print it out to the screen?
Yes.

Is there a better or faster way of doing it?
None that I can think of.
S. Rosland

3
44 points at 100%
jbagley, "Must...post...more...!" Home page   Private message  
Posted February 23 '05 at 07:25 AM
      Posts: 845
Ok, cool. Thanks Rosland. I was just checking I was doing it the right way...

4
83 points at 100%
visualAd, life is a loop Home page   Private message   E-mail
Posted February 24 '05 at 03:39 PM
      Posts: 199
What is the best way of handling them when it comes a Database driven application?

I know the methods used are stripslashes and addslashes, but what is best practice? Do you add the slashes before you insert into the database and then strip them before you print it out to the screen?

Is there a better or faster way of doing it?
Its a googd idea to check whether magic quotes is turned on, otherwise you run the risk of escaping your string twice. Something like this:
function stripslashes_safe($string)
{
    
/* why the space in the $stirng variable I don't know, 
       but the forum software insists on putting it there */
    
return get_magic_quotes_gpc()?$string:stripslashes($string);

Its also worth noting that some database management systems have different methods for escaping quotes. MS for example uses a '' to escape a quote.
Last edited February 24 '05 at 03:41 PM by visualAd. Reply

5
44 points at 100%
jbagley, "Must...post...more...!" Home page   Private message  
Posted March 8 '05 at 04:39 AM
      Posts: 845
I need some help implementing a solution to escape apostrophes and quotations.

I am running MSSQL.
$first addslashes(trim(@$_POST['firstname'])); 
This is the code I am using. If I use single quotes(') it prints this out: jason\'s, and it fails.

But if I use double quotes(") It works and prints this out: jason\"s. Here is the insert statement:
$sql "INSERT INTO ClientContact 
        (CDLID, first, surname, cellno, email, field1, field2, field3) 
        VALUES 
        ("
.$_SESSION['CDLID'].",'".$first."','".$surname."','".$cellno."','".$email."','".$field1."','".$field2."','".$field3."')"
I need a solution for both single and double quotes to work Thanks in advance
P.S. Ive turned magic quotes off.
Last edited March 8 '05 at 04:40 AM by jbagley. Reply

Post Reply