在PHP中避免SQL注入最安全的方法是什么?

ein*_*ein 2 php security sql-injection

我只是想知道这行代码是否可以安全使用以避免SQL注入?

// username and password sent from form 
$myusername=$_POST['loginUserName']; 
$mypassword=$_POST['loginPassword'];

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
Run Code Online (Sandbox Code Playgroud)

我需要条纹吗?

Wyz*_*a-- 8

使用预准备语句更安全,因此(潜在的恶意)值与查询字符串分离,而不是依赖于转义.阅读PHP数据对象.

关于stripslashes(),只有在你magic_quotes_gpc打开PHP的功能时才应该这样做,你不应该这样做,因为它已被弃用.但是,如果你想要健壮,if (get_magic_quotes_gpc()) $myusername = stripslashes($myusername);那么当且仅当添加了一个斜杠时,它会删除一层斜杠.