When we need to use PHP to insert data containing special symbols, we will find that the data can not be inserted properly, then how to solve this time? Recently encountered this problem in the work, so through the search for information has also been solved, now will deal with the method of sharing to everyone, the need for friends can refer to, learn from the following together.
Discover problems
When we write data to MySQL, if there are special characters in the data, the data can not be properly put into storage, such as:
mysql_query ("Update table set ' name ' = ' Make '");
This is usually the time addslashes()
to use this function to escape special characters in the data.
Processing methods
PHP for security, so magic_quotes_gpc = On
the introduction of a function, you can directly put single quotation marks into the database without any processing, then for off, you need to consider the single quotation mark problem, rather than blindly trust the operating environment.
When magic_quotes_gpc = On
, the use addslashes()
of the processed data in the database will be saved in the form, if the direct output at this time, you will find more than their expectations of the content of a \, so the stripslashes()
appearance, it can be removed (different from str_replace(”\”, “”,$Str)
).
When magic_quotes_gpc = Off
, the use addslashes()
of the processed data in the database will be saved in ' form, no above said there is a problem, addslashes()
play the role of inserting data without error, if the direct output at this time, the data is normal. No need to use it again stripslashes()
.
addslashes()
and stripslashes()
just the opposite, Direct memory: addslashes()
add a \, stripslashes()
go to a \
So when do you use it?
Simply put:
When magic_quotes_gpc = On
the system automatically handles problems such as single quotes, it addslashes()
's okay to use No and no stripslashes()
, but if you add data addslashes()
, you have to show the datastripslashes()
When magic_quotes_gpc = Off
the system does not handle problems such as single quotes, it must be used when inserting data addslashes()
, and it is not necessary to display the data stripslashes()
.
Since there is analysis, what to do when the program? Depending on the above two conditions, it is possible to:
Whether on or off, we use when we magic_quotes_gpc
add data, addslashes()
when on, it must be used stripslashes()
, and off is not available stripslashes()
.
The above is the whole content of this article, I hope that everyone's study has helped.