下面是一個例子查詢:不使用PDO :: PARAM可以安全地綁定嗎?
$sql = $db->prepare("INSERT INTO `accounts`
(`id`, `name`, `email`, `phoneNumber`, `country`, `city`, `addrLine1`, `company`, `zipCode`, `sha_pass_hash`, `ClientGroup`, `Newsletter`, `signedIN`) VALUES
(NULL, :name, :email, :phoneNumber, :country, :city, :addrLine1, :company, :zipCode, :sha_pass_hash, '1', '1', NOW())");
$sql->bindParam(':name',$name);
$sql->bindParam(':email',$email);
$sql->bindParam(':phoneNumber',$phoneNumber);
$sql->bindParam(':country',$country);
$sql->bindParam(':city',$city);
$sql->bindParam(':addrLine1',$addrLine1);
$sql->bindParam(':company',$company);
$sql->bindParam(':zipCode',$zipCode);
$sql->bindParam(':sha_pass_hash',$sha_pass_hash);
$sql->execute();
由於當bindParam使用PDO::PARAM
像PDO::PARAM_STR
和PDO::PARAM_INT
未設置就可以看到。
我的問題畢竟是: 當PDO::PARAM
未設置時,該查詢是否可以SQL調用?
你沒有義務來指定數據類型。如果你想要,你可以。它對安全性沒有影響,只有在值被提供給語句之前如何轉換。 – tadman
**警告**:編寫您自己的訪問控制層並不容易,並且有很多機會使其嚴重錯誤。請不要在[Laravel](http://laravel.com/)等任何現代開發框架(http://codegeekz.com/best-php-frameworks-for-developers/)上編寫自己的認證系統,內置了強大的[認證系統](https://laravel.com/docs/5.4/authentication)。絕對不會遵循[推薦的安全最佳實踐](http://www.phptherightway.com/#security),並且從不使用SHA1 **等弱散列存儲密碼。 – tadman
謝謝你的回答,但我的主要問題是「當PDO :: PARAM未設置時,此查詢是否可以SQL注入?我假設你」投票「是的,這是安全的嗎? –