tags:

views:

476

answers:

3

I don't think that was the most clear question, but an example should make it a little clearer.

I have a table filled with movie names, some of which contain apostrophes. I have a search box which is used to find movies.

If I perform searches via

mov_title = '$search_keywords'

it all works, but this method will not yield any results for partial searches, so I have to use this

mov_title LIKE '%$search_keywords%'

This method works fine for titles that are A-Za-z0-9, but if a title has an apostrophe, it's not able to find the movie, even if I do an exact match.

Before the titles are stored in the DB, I put them through this:

$search_keywords = htmlspecialchars(mysql_escape_string($_GET["search_keywords"]));

So in the DB, there is a forward slash before every single apostrophe.

The only way to match a movie title with an apostrophe is to physically put a forward slash in front of the apostrophe, in the search box.

This seems so trivial, and I'm sure the solution is painfully obvious, but I'm just not seeing it.

+5  A: 

Use mysql_real_escape_string(), and do not use htmlspecialchars(). The latter is not for database escaping, it's for HTML production.

chaos
Removing htmlspecialchars() didnt change anything. Behaves exactly the same.
Yegor
Because the values in your database already used htmlspecialchars(). You need to unescape all these values. Use html_entity_decode() and stripslashes() for instance.
Patrick Daryll Glandien
I fixed some test values, and Im still not able to find them.
Yegor
Can you give a few database rows and echo the query before using it?
Patrick Daryll Glandien
You can also consider using addslashes()
Patrick Daryll Glandien
Ah. My tech guy brilliantly turned on magic quotes, which kept multiplying the slashes. if i use the mysql_real_escape_string() before isnerting stuff into the DB, there wont be any slashes stored in there, correct?
Yegor
Do mysql_real_escape_string(stripslashes($_POST['var']))
Patrick Daryll Glandien
With magic quotes off, it doesnt add any slashes, so i think there is no need.
Yegor
Yeah if you turned them off then mysql_escape_string will suffice.
Patrick Daryll Glandien
Do not, in fact, consider using addslashes(). If you do, you will remain vulnerable to SQL injection.
chaos
+1  A: 

This only happens because you escaped the data for html output before doing the output! You should only do it right before doing the output, i.e.:

<li><?php echo htmlspecialchars($some_var); ?></li>

Unescape the values in your database and change the application to escape only on output. You currently have no other way than also doing htmlspecialchars(mysql_real_escape_string()) on the $search_string.

Even if it made sense to escape for HTML already on inserting into the database, mysql_real_escape_string() would be the outer function and not the inner function.

Patrick Daryll Glandien
A: 

If you prefer to keep your database intact, you could just run the htmlspecialchars() function on any search words you are looking for before constructing the query. Though I recommend putting the data into the database exactly as is, (but use mysql_real_escape_string() to escape it to hinder sql injection attempts). Then use htmlspecialchars() when you are printing the data to the browser.