Apostrophe troubles.

Hi all,
I am hoping someone can help me with some apostrophe troubles I am having!
I have a form where the user can enter data, that data goes into the database. I have noticed that things break when a user enters an apostrophe into some of these fields: John’s Room, for example.
I have fixed the data going into the database, but reading it from the database is still a problem.
I used the following code , as suggested by a Google search.
[php]
$name = str_replace("’", “’’”, $name);
[/php]

The problem is with the following line of code.
This line should display some database items. The user clicks on them, and textboxes are populated with db information. It will only display up to an apostrophe- ‘John’ instead of ‘John’s Room’, for example.
[php]
echo “”;
[/php]

This is the second line I am having trouble with is:
[php]
eval(“object” + i + “.addEventListener(‘click’, function () {select_event(’” + event_name + “’)});”);
[/php]
event_name is the variable with the apostrophe in it.

I have tried escaping the apostrophe with a /, but that didn’t work.
Can someone suggest a solution?
Thanks!
James. :slight_smile:

Hi theswordthatwasbroken,

The older mysql syntax had/has mysql_real_escape_string()

MySQLi has mysqli_real_escape_string()

PDO has PDO::quote

It depends on which connection you’re using would depend on which of these you would use to escape your data.

Prior to passing any data to your database you want to make sure it’s safe to run. Someone can create total havoc on your database if you don’t make sure everything is nice and clean.

You must be using MS SQL Server? That is the only one that I know of that uses a ’ to escape a '.

To display use http://us1.php.net/htmlentities with ENT_QUOTES.

Thanks for your help guys. I’m using MySQL.
I think I figured out my issues.

Sponsor our Newsletter | Privacy Policy | Terms of Service