Wednesday 6 April 2016

string - Storing values with single quote in MySQL




I started learning PHP coding earlier October, the issue which is causing my trouble is that my users need to store text in the database (and the text would probably contain single and double quotes). Whenever, I put a single quote in the text, it'd cause an error. I really need to store single and double quotes, otherwise the users would have errors when their text would contain phrases like: It's my car. For the meanwhile, I turn single quotes to double ones with str_replace.




The query which I use for inserting the text into the database is:
INSERT INTO notes (text) VALUES ('$text')



Isn't there any fix for it?


Answer



You can use a function like mysql_real_escape_string to escape the string before storing it into the database.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...