2012-04-18 91 views
-2

我想写一个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'>"; 

     } 


    ?> 

$用户我们pageCheck.php定义

+3

'的var_dump($插入);'这里显示(和现在以来始终** **检查所产生的实际的SQL ,而不是源代码) – zerkms 2012-04-18 03:41:36

+0

string(122)“INSERT INTO'vle'.'files'('id','time','fileLocation','title','user')VALUES(NULL,UNIX_TIMESTAMP() ,'','blah3','')“ – Ciaran 2012-04-18 03:45:39

+0

这是你的问题,'$ putFile'和'$ user'是空的(或未定义)。 [启用错误报告](http://stackoverflow.com/a/10135983/283366)将产生更多信息。 – Phil 2012-04-18 03:46:26

回答

3

你的问题是一个作用域之一。在您的saveFile()函数中,$user$putFile不在范围内。

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

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

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

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

+1参数是要走的路。 – 2012-04-18 03:58:50