简体   繁体   English

为什么PHP PDO不保护我的查询不被注入?

[英]Why isn't PHP PDO protecting my query from injection?

I'm still in the progress of learning PDO fully, but I was kind of surprised when I checked this evening if it worked to SQL Inject the URL parameter, and to my surprise, it did work. 我仍然在完全学习PDO,但是当我今天晚上检查它是否适用于SQL Inject the URL参数时,我感到很惊讶,令我惊讶的是,它确实有效。 So I started thinking; 所以我开始思考; the posted values are supposed to be sanitized automatically using PDO - prepared statements, which means there must be something wrong with my SQL query, am I right? 发布的值应该使用PDO自动清理,这意味着我的SQL查询一定有问题,对吗?

I'm having a page that needs a GET variable in order to gather corresponding data from my database with that ID. 我有一个需要GET变量的页面,以便从我的数据库中收集具有该ID的相应数据。 I have created a function that includes preparing the query, and as well as executing it to simplify the coding process. 我创建了一个函数,包括准备查询,以及执行它以简化编码过程。 The code I have written now looks like: 我现在编写的代码如下:

 $request = $_GET['movie'];
 $sql = "SELECT * FROM `movies` WHERE `url` = '$request'";
 $db = new database;
 $db->setDBC();

 $process = $db->executeQuery($sql);
 $cmd = $process->fetch(PDO::FETCH_NUM);

 $title = $cmd[1];

And the PDO Exception I get is: 我得到的PDO异常是:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 致命错误:带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064 SQL语法中有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''21-31282 ''' at line 1' in C:\\xampp\\htdocs\\filmvote\\include\\databaseClass.php:33 Stack trace: #0 C:\\xampp\\htdocs\\filmvote\\include\\databaseClass.php(33): PDOStatement->execute() #1 C:\\xampp\\htdocs\\filmvote\\recension.php(9): databaseManagement->executeQuery('SELECT * FROM `...') #2 {main} thrown in C:\\xampp\\htdocs\\filmvote\\include\\databaseClass.php on line 33 检查与您的MySQL服务器版本相对应的手册,以便在C:\\ xampp \\ htdocs \\ filmvote \\ include \\ databaseClass.php中的第1行''21 -31282''附近使用正确的语法:33堆栈跟踪:# 0 C:\\ xampp \\ htdocs \\ filmvote \\ include \\ databaseClass.php(33):PDOStatement-> execute()#1 C:\\ xampp \\ htdocs \\ filmvote \\ _ recension.php(9):databaseManagement-> executeQuery('SELECT * FROM` ...')在第33行的C:\\ xampp \\ htdocs \\ filmvote \\ include \\ databaseClass.php中抛出#2 {main}

You get this kind of error when adding ' or 1-1 to the URL. ' or 1-1添加到URL时会出现此类错误。 What can I do about this? 我该怎么办? Really grateful for help. 真的很感激帮助。

the posted values are supposed to be sanitized automatically using PDO 发布的值应该使用PDO自动清理

Nope. 不。 Only if you use actual prepared statements like so: 只有你使用这样的实际准备语句:

$stmt = $dbh->prepare("SELECT * FROM `movies` WHERE `url` = ?");
if ($stmt->execute(array($_GET['movie'])))  // <-- This sanitizes the value
  { 
    // do stuff
  }

will your the values you insert be automatically sanitized, and your query protected from SQL injection. 您插入的值是否会自动清理,并且您的查询将受到SQL注入的保护。

Otherwise, your SQL query will be executed like any old mysql_query() , and is vulnerable. 否则,您的SQL查询将像任何旧的mysql_query()一样执行,并且易受攻击。 PDO can not take a query and then automatically sanitize the vulnerable parts. PDO无法进行查询,然后自动清理易受攻击的部分。 That's not possible. 那是不可能的。

Try prepared statements: 尝试预备语句:

$query = $db->prepare("SELECT * FROM `movies` WHERE url = ?");
$query->execute(array($request));
$result = $query->fetch(PDO::FETCH_ASSOC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM