drxdn40242
2012-04-18 03:37
浏览 38
已采纳

MySQL不会插入变量

I'm trying to write a MySQL statement to insert information to a table:

 $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."'
               )";

Everything inserts correctly except for the $user variable which echos out but doesn't get inserted and the same goes for $putFile. Is there anything in this MySQL statement that I am doing wrong?

Thanks

full code here

    <?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 us defined in pageCheck.php

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshan9338 2012-04-18 03:55
    已采纳

    Your issue is a scoping one. Within your saveFile() function, $user and $putFile are not in scope.

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

    You should consider passing them as arguments, eg

    function saveFile($user, $file, $title) {
        // etc
    }
    
    saveFile($user, $putFile, $_POST['title']);
    
    点赞 打赏 评论

相关推荐 更多相似问题