PHP inserts a processing method that contains special symbolic data

Source: Internet
Author: User
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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.