How can I prevent SQL-injection in PHP?

in #programming8 years ago (edited)

I dont want to be hacked into oblivion

From what I understand, passing raw user input into SQL queries makes me vulnerable, for example:

$scary_stuff = $_POST['user_input']; 
mysql_query("INSERT INTO `table` (`column`) VALUES ('$scary_stuff')");

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?

Drawing on the programming gods of steem: @miohtama @romanskv @jl777 @grz @qkyrie @kkaos

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 93257.09
ETH 3124.16
USDT 1.00
SBD 3.15