我一直在阅读并询问有关sql注入安全查询的问题,每个人都说我应该使用PDO,所以我只启用了我的MysqL PDO扩展并进行了简单的查询.
所以这是我的代码:
public static function Add($catName,$catDescr = "",$catImgURL = "",$catSubLevel = 0,$catSubID = 0) { try { include_once "db_config.PHP"; $DBH = new PDO("MysqL:host=$db_host;dbname=$db_name",$db_user,$db_pass); $DBH->setAttribute( PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING ); $STH = $DBH->prepare("INSERT INTO cat (catName,catDescr,catImg,catSubLevel,catSubID) VALUES ('$catName','$catDescr','$catImgURL',$catSubLevel,$catSubID)"); $STH->execute(); } catch (PDOException $e) { echo $e->getMessage(); } }
所以一切正常并且看似安全,但是当我做这样的事情时:
Cat::Add("Test Cat","' OR 1==1 --");
它给了我
Warning: PDOStatement::execute()
[pdostatement.execute]:
sqlSTATE[42000]: Syntax error or
access violation: 1064 You have an
error in your sql Syntax; check the
manual that corresponds to your MysqL
server version for the right Syntax to
use near ‘==1 –‘,”,0)’ at line
2 in www\mCat.PHP on line
25
我想这是因为我添加了$DBH-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_WARNING);为了看我什么时候有错误.