PHP simple way to prevent SQL injection _php tips

Source: Internet
Author: User

This article illustrates a simple way to prevent SQL injection in PHP. Share to everyone for your reference, specific as follows:

SQL injection is generally due to the irregular syntax, the problem appears on the SQL statement, and the decisive is quote ('). As follows:

$sql = "Delete from table where id = ' $id '";

The normal submission is to delete a piece of data, if the ID is submitted (1 ' or 1 #), then the SQL statement becomes a

Delete from table where id = ' 1 ' or 1 # ';

This will erase the entire table, resulting in irreparable results.

Now that the problem appears on the quote, just escape it (\)

PHP provides two functions to use

Addslashes ($STR)
//recommended use the following to avoid character set problems
mysql_real_escape_string ($str, $link)
//Avoid integer data may not be added by SQL quotes, Forces the converted data to use quotation marks to wrap
function ($str) {return
  "'". Mysql_real_escape_string ($str, $this->link). "'";
}

More about PHP Interested readers can view the site topics: "PHP Design Security Course", "PHP Security Filtering Skills Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", " PHP Operations Office Document Tips summary (including word,excel,access,ppt), "PHP Introduction to Object-oriented Programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Introductory Tutorial" and " A summary of common PHP database operations tips

I hope this article will help you with the PHP program design.

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.