MySQL不会插入变量

时间:2012-04-18 03:37:35

标签: php mysql

我正在尝试编写一个MySQL语句来向表中插入信息:

 $insert = "INSERT INTO `vle`.`files`
               (`id`, `time`, `fileLocation`, `title`, `user`)

               VALUES (
                   NULL,
                   UNIX_TIMESTAMP(),

                   '".mysql_real_escape_string($putFile)."',
                   '".mysql_real_escape_string($_POST['title'])."',
                   '".$user."'
               )";

除了$user变量之外,其他所有内容都正确插入,这些变量回显但未插入,$putFile也是如此。这个MySQL声明中有什么东西我做错了吗?

由于

这里的完整代码

    <?php require_once('Connections/localhost.php');
    include('pageCheck.php');
    include('adminCheck.php');

    function saveFile(){
        global $_FILES, $_POST;

        $insert = "INSERT INTO `vle`.`files` 
(`id`, `time`, `fileLocation`, `title`, `user`) 
VALUES (NULL, UNIX_TIMESTAMP(), '".mysql_real_escape_string($putFile)."', '".mysql_real_escape_string($_POST['title'])."', '".$user."')";

        mysql_query($insert);

        var_dump($insert);
    }

    $putFile = "files/".basename($_FILES['uploadedFile']['name']);

    if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $putFile)){
        saveFile();
        echo"File has been uploaded, Redirecting to file list now...";
        //echo"<meta http-equiv='refresh' content='3;url=dashboard.php'>";

        }else{

        echo"Unable to upload file, Returning to dashboard...";
        //echo"<meta http-equiv='refresh' content='3;url=dashboard.php'>";  

        }


     ?>

$ user我们在pageCheck.php中定义

1 个答案:

答案 0 :(得分:3)

您的问题是范围问题。在saveFile()函数中,$user$putFile不在范围内。

请参阅http://php.net/manual/en/language.variables.scope.php

你应该考虑将它们作为参数传递,例如

function saveFile($user, $file, $title) {
    // etc
}

saveFile($user, $putFile, $_POST['title']);