PHP convert ' to html code

715

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

294

Answer

Solution:

Useaddslashes() or mysql_real_escape_string()

897

Answer

Solution:

Usehtmlentities andhtmlspecialchars 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.

324

Answer

Solution:

what about

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

Answer

Solution:

Use mysql_real_escape_string($str) while inserting in database.

People are also looking for solutions to the problem: php - how can i put file to a certain google drive account?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.