Apostrophes Not Supported on MySql?

Apostrophes Not Supported on MySql?

Posted by: ToneHaven
Posted on: 2006-08-12 07:09:00

Hello I'm new to Dreamhosts,
I have set up a website & MySql here:
http://www.appareltennis.info/

But when I try to login and enter data (edit or add new article) it does not let me enter if the text has an apostrophe. No error is given and I will just be sent back to page as if it was successful .

If I try it with out apostrophes then it will work.

I also ran a duplicate of this site on my last host and never had this happen.

Any help would be good.

Re: Apostrophes Not Supported on MySql?

Posted by: norm1037
Posted on: 2006-08-12 07:34:00

The apostrophe needs to be escaped with a backslash depending on whether or not magic quotes is set to on or off. Which is why it works at one host and not another.

Do a Google search for 'apostrophe mysql' if you wish to read up about it and addslashes/stripslashes.

You can start at this article...
http://www.webmasterstop.com/63.html



--
Norm


Opinions are my own views, not DreamHosts'.
I am NOT a DreamHost employee OK!! mad

You act on my advice at your own risk!

Re: Apostrophes Not Supported on MySql?

Posted by: ToneHaven
Posted on: 2006-08-12 08:08:00

I added the following like the articles said to the main config file of my site ( I also tried it on index.php):

( "<?php

function myAddSlashes( $string ) {

if (get_magic_quotes_gpc()==1) {

return ( $string );

} else {

return ( addslashes ( $string ) );

}

}

?>" );


but nothing changed and tried to add a sentence with the apostrophe but it didn't enter again.

I have done something wrong I guess?

Re: Apostrophes Not Supported on MySql?

Posted by: seiler
Posted on: 2006-08-12 09:28:00

I only took a quick glance at that page--but I don't see where it ever mentions calling the function. Defining a function, by itself, doesn't do anything.

Where ever you're inserting the string into the DB, you'll need to call the function, something like myAddSlashes($your_string_name) before the insert.

So, if you're working with a POST variable, like "user_name" you could do something like:

$user_name = myAddSlashes($_POST['user_name']);

Then, the string $user_name should be ready to insert.

I'd give that a try.

Or, if your script is already set up to run a certain way, you might want to scroll down on that page to the section about adding a line to your .htaccess file or setting it in the php.ini.

Re: Apostrophes Not Supported on MySql?

Posted by: norm1037
Posted on: 2006-08-12 09:58:00

How many apostrophes will you have to input? If it is just you inputting can you use /' and just escape each apostrophe until the script is sorted out?



--
Norm


Opinions are my own views, not DreamHosts'.
I am NOT a DreamHost employee OK!! mad

You act on my advice at your own risk!

Re: Apostrophes Not Supported on MySql?

Posted by: scjessey
Posted on: 2006-08-12 10:41:00

In reply to:

If it is just you inputting can you use /' and just escape each apostrophe until the script is sorted out?


They should be escaped with a back slash "", not a forward slash.

Re: Apostrophes Not Supported on MySql?

Posted by: norm1037
Posted on: 2006-08-12 11:36:00

Can we meet halfway |' smile

I was waiting to see who would notice my deliberate mistake first. laugh


--
Norm


Opinions are my own views, not DreamHosts'.
I am NOT a DreamHost employee OK!! mad

You act on my advice at your own risk!

Re: Apostrophes Not Supported on MySql?

Posted by: seiler
Posted on: 2006-08-12 16:15:00

In reply to:

Can we meet halfway |'



The Upright Slash: For those embarrassing moments when you're not sure if a forward or backward slash is appropriate.

;)

Re: Apostrophes Not Supported on MySql?

Posted by: ToneHaven
Posted on: 2006-08-12 16:46:00

I thought I would try changing the httaccess file like this

<?php
highlight_string ( "<IfModule mod_php4.c>
php_flag magic_quotes_gpc on
</IfModule>" );
?>

I changed the Off to On

Nothing happened, I also tried for Php5 as I'm unsure what version is running and still no change so reverted back now.

I'm unsure how I would insert what seiler has said :

$user_name = myAddSlashes($_POST['user_name']);


My config page is nearly 2000 lines and I have no idea where I would use this?




Re: Apostrophes Not Supported on MySql?

Posted by: ToneHaven
Posted on: 2006-08-12 16:49:00

<<<< How many apostrophes will you have to input? If it is just you inputting can you use /' and just escape each apostrophe until the script is sorted out?>>>>

I would be entering lots of pages of articles with many apostrophes but I also have lots of other sites I wanted to add that will also use apostrophes in lots of articles so as you can see adding a slash each time would be a major PITA.

I'm surprised that others don't have this problem?

Re: Apostrophes Not Supported on MySql?

Posted by: ToneHaven
Posted on: 2006-08-13 03:34:00

this is still unresolved, any ideas ?

Re: Apostrophes Not Supported on MySql?

Posted by: norm1037
Posted on: 2006-08-13 03:43:00

It has cropped up before. I had the same problem with a guestbook script I wrote and had problems with "O'Hara" (not the person, the name.) so I just amended the code where I needed input into the database. That was fairly easy since there were just a few lines.

I used:-

$NameLast = addslashes($_POST['NameLast']);

(So names like O'Hara were stored as O'Hara)

in the input code phase .


Then when reading the database back to screen used:-

echo stripslashes($row["NameLast"]);

(So records like O'Hara were displayed as O'Hara)



Have you tried switching to PHP4 to see if that helps. You can change back and forth within 'Manage|Domains>[edit]'. I found I did not have to use add/stripslash under PHP4 because of the default setting of the magic_quotes setting.

I should add of course that the default settings of PHP5 are better. smile

Later:
I reread your replies and see you tried the PHP4/5 thing so scrub that...


This is a bit more of the input code to show where I added addsash

$NameLast = addslashes($_POST['NameLast']);
$NameFirst = $_POST['NameFirst'];
$Email = $_POST['Email'];
$Skills = $_POST['Skills'];
$IP = getenv('REMOTE_ADDR');
$dateshow = date('dmy His');



With that huge amount of code you will have to try and strike lucky and find the correct variable that sends input into the database.
Alternatively have a Google for "magic quotes setting htaccess" which might help... or not!





--
Norm


Opinions are my own views, not DreamHosts'.
I am NOT a DreamHost employee OK!! mad

You act on my advice at your own risk!Edited by norm1037 on 08/13/06 04:23 AM (server time).

Re: Apostrophes Not Supported on MySql?

Posted by: adityagaddam
Posted on: 2006-08-13 06:18:00

Oh please god, don't use magic quotes. PHP has a nice function that takes care of this.

http://us3.php.net/manual/en/function.mysql-real-escape-string.php

You just call that function on your string before you insert/update.

When you retrieve the data, you don't need to do anything, it just works.

Tags: mysql