【问题标题】:$user_id = $GET['user_id'] returns null when upload file$user_id = $GET['user_id'] 上传文件时返回 null
【发布时间】:2014-07-04 16:18:25
【问题描述】:

当我将文件上传到我的数据库时,我希望它获取 user_id,但它返回 null。

我已经做了一些测试来检查它是否真的返回 null 并且一旦我上传。

它知道该上传页面上登录了哪个 user_id。

查看我的代码:

<?php

if(isset($_POST['title']))
{
    //Check file type
    $ext = substr(strrchr(basename($_FILES['file']['name']), '.'), 1);
    if (!in_array($ext, $okExts))
    {
        echo "<p class=\"bad\">Formato Inválido.</p>";
    }
    else
    {
        $movePath = "../".$uploadPath . basename($_FILES['file']['name']); 
        $url = basename($_FILES['file']['name']);
        $title = mysql_escape_string($_POST['title']);
        $description = mysql_escape_string($_POST['description']);
        $user_id = $_GET['user_id'];

        if(move_uploaded_file($_FILES['file']['tmp_name'], $movePath)) 
        {
            //Update Fields
            $fileSQL = "INSERT INTO files (title, description, user_id, url) 
                      VALUES ('$title', '$description', '$user_id','$url')";

            mysql_query($fileSQL,$conn) or die('Error: ' . mysql_error());

            echo "<p class=\"good\">O Arquivo ".basename( $_FILES['file']['name'])." foi enviado.</p>";
        } 
        else
        {
            echo "Ocorreu um erro ao enviar o arquivo, por favor tente novamente!";
        }
    }
}
?>

【问题讨论】:

  • 我认为您在运行此之前已经从数据库中获取用户 ID?我看到 GET 和 POST 在一起,我感到畏缩。
  • 是的,我已经从数据库中获取用户 ID。
  • 那么,问题可能不在于您发布的代码,而在于您从中获取它的代码。 var_dump($user_id); 放在$user_id = $_GET['user_id']; 之后会显示什么? (在您发布的代码中)。
  • 使用请求和检查
  • 这看起来非常不安全。你确定你的用户参数是properly escaped吗?

标签: php mysql sql mysqli userid


【解决方案1】:

你为什么不应该使用 $_POST['user_id'];你是否 $_POST['title'].获取 $_GET['user_id'];您应该使用 URL 传递 page.php?user_id=36525。

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 2018-03-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    相关资源
    最近更新 更多