我在 PHP 中使用 PDO。每当我传递 $id = "2abcd"(带字符串后缀的数字)时,查询都会成功返回。 (数据为$id=2)
在数据库中:id INT(11), img VARCHAR(100), uname VARCHAR(100)
public static function getImagePath($id) {
$sql_fetch_opp = "select img from my_user where id=:id";
$db = DAO::connect();
try {
$stmt = $db->prepare($sql_fetch_opp);
$stmt->bindParam(':id', $id);
$stmt->execute();
$imgPath = $stmt->fetch(PDO::FETCH_COLUMN);
return $imgPath;
} catch (PDOException $e) {
Err::log($e);
throw new MyException(Err::PDO_EXCPTION);
}
}
最佳答案
您只需要检查提供的值是否与将其转换为整数后的值相同。如果不是,则抛出异常。
public static function getImagePath($id) {
$sql_fetch_opp = "select img from my_user where id=:id";
$db = DAO::connect();
try {
if((string)$id !== (string)(int)$id)
{
throw new PDOException("Invalid id provided");
}
$stmt = $db->prepare($sql_fetch_opp);
$stmt->bindParam(':id', $id);
$stmt->execute();
$imgPath = $stmt->fetch(PDO::FETCH_COLUMN);
return $imgPath;
} catch (PDOException $e) {
Err::log($e);
throw new MyException(Err::PDO_EXCPTION);
}
}
关于php - PDO PHP 字符串输入以数字 ("2abcd"开始)在 $stmt->bindParam ("2", $id) 中被修剪为只有数字 (':id' );,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303870/