Home  >  Article  >  Backend Development  >  How to insert data containing special symbols in php

How to insert data containing special symbols in php

墨辰丷
墨辰丷Original
2018-05-30 15:28:021776browse

When we need to use php to insert data containing special symbols, we will find that the data cannot be inserted normally, so how to solve it at this time? I encountered this problem at work recently, so I solved it by searching for information. Now I will share the solution with you. Friends in need can refer to it. Let's learn together.

Found the problem

When we write data to mysql, if there are special characters in the data, the data will not be stored properly. situation, for example:

mysql_query(”update table set `name`='make's'”);

At this time, the addslashes() function is generally used to escape the special characters in the data

Processing method

For the sake of security, PHP has introduced a magic_quotes_gpc = On function, you can Single quotes can be directly inserted into the database without any processing. So when it comes to Off, you need to consider the issue of single quotes instead of blindly trusting the operating environment.

When magic_quotes_gpc = On, the processed data using addslashes() will be saved in the database in the form of \'. If it is directly When outputting, you will find that there are more \ than you expected, so stripslashes() appears, it can remove \ (different from str_replace("\", "",$ Str)).

When magic_quotes_gpc = Off, the data processed using addslashes() will be saved in the database in the form of ', without the above mentioned \ problem, addslashes() plays the role of inserting data without errors. If it is output directly at this time, the data will be normal. No need to use stripslashes() anymore.

addslashes() and stripslashes() are exactly the opposite. Direct memory: addslashes()Add a \,stripslashes()Go to one\

So when should I use it?

Simply put:

When magic_quotes_gpc = On, the system will automatically handle issues such as single quotes, whether to use addslashes() and stripslashes() do not matter, but if addslashes() is used when adding data, then stripslashes()## must be used when displaying data

#When

magic_quotes_gpc = Off, the system will not handle issues such as single quotes, so you must use addslashes() when inserting data to display the data There is no need to use stripslashes() .

Now that we have analysis, what should we do when doing the program? According to the above two situations, we can get:


No matter whether

magic_quotes_gpc is On or Off, we use addslashes() when adding data. When On , stripslashes() must be used, and stripslashes() cannot be used when Off.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP implementation of time addition and subtraction function strtotime usage

phpHow to implement a simple web calculator function

PHP method to find the factorial of a number based on a recursive function

The above is the detailed content of How to insert data containing special symbols in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn