Weird issue with mysql_real_escape_string

I’m having a problem when using mysql_real_escape_string to allow apostrophes to be inputted into fields in a mySQL db.

Basically it cuts off the value of the data after the first instance of an apostrophe. So “Testing testing’s test testtesting” would be truncated to “Testing testing” and so forth.

I’m using this code:


$description = mysql_real_escape_string($_POST['_Description']);

And the query (which otherwise works fine) is:


$query = "UPDATE userdata SET RealName = '$realname', EmailAddress = '$emailaddress', YearOfBirth = '$yearofbirth', Profession = '$profession', Description = '$description' WHERE UserName = '$username'";

Any idea why it’s doing this?

Thanks

Now solved- it was the actual output into the edit form that wasn’t working, it was fine in the db. Used htmlentities to fix. :slight_smile:

May I ask, what methodology did you use to pinpoint your error?

Hi Cups, well I checked the actual value in the db in phpMyAdmin and saw that mysql_real_escape_string was actually doing it’s job and the data was going in correctly. However when it was being displayed back out using the “Edit details” form, it was cutting off at the first apostrophe- so I used htmlentities thus:


htmlentities($description, ENT_QUOTES);

Where $description was the variable for the Description field containing text with apostrophes.

Hopefully this will help anyone who has a similar issue at any point. :slight_smile:

Ah, right. I imagined you had not quoted the HTML form element or something like that.

I hope your experience leads to you helping someone else to work out what goes wrong between html/php/sql – it happens all the time on here.

Isn’t that what Fou-Lou advised you to do a couple of days ago on the other forum? - That being the case, why did you then come here and ask for more help?

Simply put, I posted on both forums in order to help increase the chance of response- with a deadline looming. Seeing as you’re interested.

My apologies, having just checked over there I see you started them both on the same day/time. I thought I’d seen that topic two days ago - clearly I was wrong!

I’m very sorry.