Skip to main content
The 2024 Developer Survey results are live! See the results
added 1 character in body; edited title
Source Link
Ry-
  • 222.9k
  • 55
  • 483
  • 489

How can I prevent SQL-injection injection in PHP?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injectionSQL injection, like in the following example:

$unsafe_variable = $_POST['user_input']; 

mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')");

That's because the user can input something like value'); DROP TABLE table;--, and the query becomes:

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

What can be done to prevent this from happening?

How can I prevent SQL-injection in PHP?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:

$unsafe_variable = $_POST['user_input']; 

mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')");

That's because the user can input something like value'); DROP TABLE table;--, and the query becomes:

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

What can be done to prevent this from happening?

How can I prevent SQL injection in PHP?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:

$unsafe_variable = $_POST['user_input']; 

mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')");

That's because the user can input something like value'); DROP TABLE table;--, and the query becomes:

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

What can be done to prevent this from happening?

Post Locked by Robert Harvey
Notice added Wiki Answer by Robert Harvey
This question was first posed to address PHP specifically, and the answers reflect that. **This is not a canonical answer for any language.** Add PHP back in...
Link
user456814
user456814

How can I prevent SQL injection-injection in PHP?

Removed horizontal scrollbars, it's distracting.
Source Link
user456814
user456814

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:

$unsafe_variable = $_POST['user_input']; 

mysql_query("INSERT INTO `table` (`column`) VALUES ('" . $unsafe_variable . "''$unsafe_variable')");

That's because the user can input something like value'); DROP TABLE table;--, and the query becomes:

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

What can be done to prevent this from happening?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:

$unsafe_variable = $_POST['user_input']; 

mysql_query("INSERT INTO `table` (`column`) VALUES ('" . $unsafe_variable . "')");

That's because the user can input something like value'); DROP TABLE table;--, and the query becomes:

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

What can be done to prevent this from happening?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:

$unsafe_variable = $_POST['user_input']; 

mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')");

That's because the user can input something like value'); DROP TABLE table;--, and the query becomes:

INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')

What can be done to prevent this from happening?

Removed tag from title.
Link
axwcode
  • 7.8k
  • 7
  • 31
  • 41
Loading
Removed literal ** from code block since not interpreted as markdown inside code block.
Source Link
toxalot
  • 11.6k
  • 6
  • 38
  • 59
Loading
Added grave accent's to query's
Source Link
Companjo
  • 1.8k
  • 19
  • 24
Loading
Rollback to Revision 38
Source Link
Josh Crozier
  • 238.8k
  • 56
  • 399
  • 312
Loading
Added more explanation.
Source Link
Madurai
  • 297
  • 3
  • 16
Loading
Rollback to Revision 36
Link
Your Common Sense
  • 157.6k
  • 42
  • 220
  • 354
Loading
Removed mysql tag
Link
Sajad Karuthedath
  • 15.4k
  • 4
  • 33
  • 49
Loading
Replaced sentence with a more appropriate version.
Source Link
Andrew
  • 3.6k
  • 4
  • 27
  • 45
Loading
added 12 characters in body
Source Link
Chris Cooper
  • 17.4k
  • 10
  • 54
  • 70
Loading
added 1 characters in body
Source Link
sybear
  • 7.8k
  • 1
  • 23
  • 38
Loading
deleted 54 characters in body
Source Link
tckmn
  • 58.8k
  • 27
  • 116
  • 156
Loading
added 4 characters in body
Source Link
Chris Seymour
  • 85k
  • 31
  • 164
  • 206
Loading
deleted 2 characters in body
Source Link
웃웃웃웃웃
  • 11.9k
  • 15
  • 62
  • 92
Loading
Edited with code tags
Source Link
Joran Den Houting
  • 3.2k
  • 4
  • 22
  • 51
Loading
added 2 characters in body
Source Link
j0k
  • 22.7k
  • 28
  • 80
  • 90
Loading
changed back to mysql_* changing it to mysqli_* does NOTHING....
Source Link
Naftali
  • 145.9k
  • 40
  • 246
  • 304
Loading
edited tags
Link
user823738
  • 17.3k
  • 8
  • 53
  • 78
Loading
edited title
Link
yoozer8
  • 7.5k
  • 7
  • 61
  • 96
Loading
Changed mysql to mysqli
Source Link
pattyd
  • 6.1k
  • 11
  • 39
  • 57
Loading