简体   繁体   English

SQL Select不基于查询检索数据

[英]SQL Select not retrieving data based on query

I'm trying to push a file to browser from SQL via the use of the file token which is created and assigned to the file during the upload process. 我试图通过使用在上载过程中创建并分配给文件的文件令牌将文件从SQL推送到浏览器。 But my SELECT SQL is not working for anything other then file id field which is the only one that seems to fire up SELECT request here is the code 但是我的SELECT SQL不能用于文件ID字段以外的其他任何东西,这是唯一似乎触发SELECT请求的代码,这里是代码

$item = $_GET['item'];

$sql = 'SELECT * FROM `files` WHERE file_token = '.$item.'';
            $result = mysql_query($sql);

            if(!$result) {

                echo '<div style="padding:8px;background-color:#fae3e3;border:2px solid #b25959;color:#313131;">Error!</div>';

             } else {

                  while($obj = mysql_fetch_array($result)) {

                                     $file_type = $obj['file_type'];
                                     $file_size = $obj['file_size'];
                                     $file_name = $obj['file_name'];
                                     $file_hash = $obj['file_hash'];

                                     $name = 'encrypted/'.$file_hash;

if (file_exists($name)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file_name));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($name));
    ob_clean();
    flush();
    readfile($name);
    exit;
}

                   }
            }
mysql_query("UPDATE `files` SET file_views = file_views+1 WHERE file_token = '.$item.'");

mysql_close();

Is there something wrong with my SELECT statement? 我的SELECT语句有问题吗? And tokens look like this in SQL 在SQL中令牌看起来像这样

Example: 3ed3:3ba6:eb24:5816:6d8b:be06:79e1:b20b 示例:3ed3:3ba6:eb24:5816:6d8b:be06:79e1:b20b

尝试:

$sql = 'SELECT * FROM `files` WHERE file_token = \''.$item.'\'';

try to put quotes arround the $item 尝试在$ item周围加上引号

$sql = 'SELECT * FROM files WHERE file_token = "'.$item.'"'; $ sql ='选择*从files所在的file_token =“'。$ item。'”';;

As others have pointed out your issue has to do with no properly quoting strings when querying the database. 正如其他人指出的那样,您的问题与查询数据库时没有正确引用字符串有关。 That said there is a more fundamental issue at play with SQL Injection in general. 那就是说,一般来说, SQL Injection有一个更根本的问题。 Let's take a look at your first two lines: 让我们看一下您的前两行:

$item = $_GET['item'];
$sql = 'SELECT * FROM `files` WHERE file_token = '.$item.'';

$item is set to whatever the user feels like providing to you and then directly dropped into your query. $item设置为用户希望提供给您的任何内容,然后直接放入您的查询中。 If the user is polite and sends along an actual file token everything ends up nice: 如果用户彬彬有礼并发送了实际的文件令牌,那么一切都会很好:

// http://example.com/yourapp.php?item=5
SELECT * FROM `files` where file_token = 5;

What if the user sends in some random string of data instead? 如果用户改为发送一些随机数据字符串怎么办?

// http://example.com/yourapp.php?item=John%20Doe
SELECT * FROM `files` where file_token = John Doe;

The above SQL would be invalid. 上面的SQL将无效。 That string would need to have quotes around it such as: 该字符串将需要在其周围加上引号,例如:

SELECT * FROM `files` where file_token = "John Doe";

Editing your code to simply add the quotes may seem like enough of a solution, but it isn't. 编辑代码以简单地添加引号似乎足以解决问题,但事实并非如此。 If we look at a solution like: 如果我们看一个像这样的解决方案:

$sql = 'SELECT * FROM files WHERE file_token = "'.$item.'"';

We will indeed add quotes around whatever is passed in by the user. 实际上,我们将围绕用户传递的内容添加引号。 So in the example of John Doe we would get the proper SQL with John Doe quoted. 因此,在John Doe的示例中,我们将获得引用John Doe的正确SQL。 What if the user decides instead that they wish to submit a GET request with the term 5"; TRUNCATE TABLE files; "; 如果用户决定自己希望提交带有术语5"; TRUNCATE TABLE files; ”;的GET请求,该怎么办?

our SQL would end up looking like: 我们的SQL最终看起来像:

SELECT * FROM files WHERE file_token = "5"; TRUNCATE TABLE files; "";

The single query now becomes 3: 现在,单个查询变为3:

SELECT * FROM files WHERE file_token = "5";
TRUNCATE TABLE files; 
"";

You could try to go farther by stipping out semi-colons or similar; 您可以尝试通过分号或类似内容来走得更远。 but there's no need to reinvent the wheel. 但无需重新发明轮子。 Check out this great SO answer for specifics on preventing SQL injection in PHP. 查看此出色的SO答案,以获取有关防止PHP中进行SQL注入的详细信息。

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

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