-2

我正在尝试编写一个 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 中定义

4

1 回答 1

3

您的问题是范围界定问题。在您的saveFile()职能范围内,$user并且$putFile不在范围内。

http://php.net/manual/en/language.variables.scope.php

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

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

saveFile($user, $putFile, $_POST['title']);
于 2012-04-18T03:55:40.427 回答