0

My string $name = "Ali'Shan"; I want store it into database but the ' I use htmlentities/ htmlspecialchars and str_replace but insert I still get syntax error ' .

$name = "Ali'Shan";
str_replace("'", "", $name);
echo $name;

echo htmlentities($name);

My output is still Ali'Shan

5
  • 1
    ' is not an entity for HTML purposes. It's a legitimate character. For putting into the database you use mysql_real_escape_string() or the appropriate equivalent for your DB. Commented Dec 28, 2013 at 11:27
  • addslashes() or mysql_real_escape_string() Commented Dec 28, 2013 at 11:28
  • Thanks mysql_real_escape_string() solved the problem. Commented Dec 28, 2013 at 11:32
  • Don't use mysql_real_escape_string, it forces you to use the mysql_ library, which is deprecated and will be removed from PHP. There are better alternatives. Commented Dec 28, 2013 at 11:33
  • You can use mysqli::real_escape_string it's an alternative like Quentin meant. Commented Dec 28, 2013 at 12:07

4 Answers 4

1

Use addslashes() or mysql_real_escape_string()

Sign up to request clarification or add additional context in comments.

1 Comment

Don't use addslahes, it is simply unsuitable. Don't use mysql_real_escape_string, it forces you to use the mysql_ library, which is deprecated and will be removed from PHP. There are better alternatives.
1

Use htmlentities and htmlspecialchars when you want to insert text into an HTML document.

A database is not an HTML document. You need to use the appropriate mechanisms for adding text to an SQL query.

For the most part, those mechanisms are prepared statements with bound variables.

Comments

0

what about

str_replace("'", "\'", $name);

Comments

0

Use mysql_real_escape_string($str) while inserting in database.

1 Comment

Don't use mysql_real_escape_string, it forces you to use the mysql_ library, which is deprecated and will be removed from PHP. There are better alternatives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.