2012-04-18 93 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