Skip to main content
Change the params in mysqli_real_escape_string to the right way around (dbconn, value)
Source Link
Can O' Spam
  • 2.8k
  • 4
  • 21
  • 47

Security Warning: This answer is not in line with security best practices. Escaping is inadequate to prevent SQL injection, use prepared statements instead. Use the strategy outlined below at your own risk.

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($dbConnection, $_POST["user-input"], $dbConnection);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

Security Warning: This answer is not in line with security best practices. Escaping is inadequate to prevent SQL injection, use prepared statements instead. Use the strategy outlined below at your own risk.

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($_POST["user-input"], $dbConnection);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

Security Warning: This answer is not in line with security best practices. Escaping is inadequate to prevent SQL injection, use prepared statements instead. Use the strategy outlined below at your own risk.

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($dbConnection, $_POST["user-input"]);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

added 327 characters in body
Source Link
miken32
  • 42.6k
  • 16
  • 119
  • 164

Security Warning: This answer is not in line with security best practices. Escaping is inadequate to prevent SQL injection, use prepared statements instead. Use the strategy outlined below at your own risk.

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($_POST["user-input"], $dbConnection);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($_POST["user-input"], $dbConnection);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

Security Warning: This answer is not in line with security best practices. Escaping is inadequate to prevent SQL injection, use prepared statements instead. Use the strategy outlined below at your own risk.

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($_POST["user-input"], $dbConnection);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

Totally Code Updated to Mysqli Version (PHP > 7.0)
Source Link
M Uzair Qadeer
  • 492
  • 1
  • 8
  • 19

Deprecated Warning: This answer's sample code (like the question's sample code) uses PHP's MySQL extension, which was deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0.

Security Warning: This answer is not in line with security best practices. Escaping is inadequate to prevent SQL injection, use prepared statements instead. Use the strategy outlined below at your own risk. (Also, mysql_real_escape_string() was removed in PHP 7.)

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($_POST["user-input"], $dbConnection);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

Deprecated Warning: This answer's sample code (like the question's sample code) uses PHP's MySQL extension, which was deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0.

Security Warning: This answer is not in line with security best practices. Escaping is inadequate to prevent SQL injection, use prepared statements instead. Use the strategy outlined below at your own risk. (Also, mysql_real_escape_string() was removed in PHP 7.)

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($_POST["user-input"], $dbConnection);
mysqli_query("INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

You could do something basic like this:

$safe_variable = mysqli_real_escape_string($_POST["user-input"], $dbConnection);
mysqli_query($dbConnection, "INSERT INTO table (column) VALUES ('" . $safe_variable . "')");

This won't solve every problem, but it's a very good stepping stone. I left out obvious items such as checking the variable's existence, format (numbers, letters, etc.).

Added appropriate warnings.
Source Link
Theodore R. Smith
  • 22.7k
  • 13
  • 68
  • 92
Loading
Spelling
Source Link
halfer
  • 20.2k
  • 19
  • 105
  • 197
Loading
corrected command syntax
Source Link
Ajay Singh
  • 733
  • 9
  • 21
Loading
Rollback to Revision 5 - Last edit mostly plagiarized from https://www.cloudways.com/blog/protect-php-website-sql-injection/
Source Link
Pang
  • 9.9k
  • 146
  • 85
  • 124
Loading
Deprecated extension removed, Expanded the Answer with more specific theories
Source Link
Kiran Maniya
  • 8.8k
  • 10
  • 60
  • 84
Loading
Deprecated extension removed
Source Link
Kiran Maniya
  • 8.8k
  • 10
  • 60
  • 84
Loading
Post Made Community Wiki by animuson
Add a note about the insecurity of escaping.
Source Link
Scott Arciszewski
  • 34k
  • 17
  • 91
  • 212
Loading
(its = possessive, it's = "it is" or "it has". See for example <http://www.wikihow.com/Use-Its-and-It's>.) In English, the subjective form of the singular first-person pronoun, "I", is capitalized, along with all its contractions such as I'll and I'm.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
Code formatting.
Source Link
Morten Kristensen
  • 7.6k
  • 4
  • 33
  • 52
Loading
Source Link
Tanerax
  • 5.8k
  • 5
  • 29
  • 28
Loading