Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I safe from a MySQL injection? [duplicate]

Is the following good enough to avoid a SQL injection?

mysql_real_escape_string(htmlentities (urlencode($_POST['postmessage'])));
like image 483
user342391 Avatar asked Aug 28 '10 15:08

user342391


People also ask

Is SQL injection a risk?

SQL injection attacks pose a serious security threat to organizations. A successful SQL injection attack can result in confidential data being deleted, lost or stolen; websites being defaced; unauthorized access to systems or accounts and, ultimately, compromise of individual machines or entire networks.

Are SQL injection attacks common?

SQL injection, also known as SQLI, is a common attack vector that uses malicious SQL code for backend database manipulation to access information that was not intended to be displayed. This information may include any number of items, including sensitive company data, user lists or private customer details.

Is PDO safe from SQL injection?

The short answer is NO, PDO prepares will not defend you from all possible SQL-Injection attacks.

Does MySQL prevent SQL injection?

Conclusions. Parameterized queries solve SQL Injection vulnerabilities. This example uses PDO to fix the vulnerability but you can still use mysqli functions to prevent SQL Injection.


1 Answers

mysql_real_escape_string() is the only method you need here.

You shouldn't do an htmlentities() nor urlencode() before inserting data in your database. These methods are usually code executed during the rendering of the view you offer to your users.

A better way to avoid SQL injection is the use of prepared statements.


Resources:

  • php.net - Prepared statements with PDO

On the same topic:

  • PHP: Is mysql_real_escape_string sufficient for cleaning user input?
like image 110
Colin Hebert Avatar answered Sep 19 '22 00:09

Colin Hebert