2011-08-01 53 views
0

我已经使用了PDO:如何清理php中的数据以避免SQL注入?

 $stmt = $aPDO->prepare("INSERT INTO ".$this->getM_oUser()->getM_sTableName()." (email, hash_pw, hash_key) VALUES (:email, :hash_pw, :hash_key)");            
     $stmt->bindValue(':email', $this->getM_oUser()->getM_sEmail()); 
     $stmt->bindValue(':hash_pw', $this->getM_oUser()->getM_sHash_pw()); 
     $stmt->bindValue(':hash_key', $this->getM_oUser()->getM_sHash_Key()); 

     $stmt->execute(); 

是否也应该使用mysql_real_escape_string()来处理用户输入的字符串?谢谢。

+0

的可能重复[PDO是准备充分,以防止SQL注入语句?(HTTP://计算器的.com /问题/ 134099/ARE-PDO-制备语句-足以对防止-SQL注入) –

回答

1

使用带有绑定参数的预准备语句就足够了。你不需要使用mysql_real_escape_string(如果你想要的话,你可能无法做到 - 你需要一个MySql连接资源来做到这一点)。

0

我会做这样的事情,从你的表名排除了很多无用的字符:

$tableName = '`' . preg_replace('`[^-a-zA-Z0-9_]`', $this->getM_oUser()->getM_sTableName()).'`'; 
$stmt = $aPDO->prepare("INSERT INTO ".$tableName." (email, hash_pw, hash_key) VALUES (:email, :hash_pw, :hash_key)"); 
相关问题