2012-10-26 64 views
2

嘿,夥計們我有一個大問題,我不知道爲什麼.. 我有幾個窗體上傳文件到數據庫,他們都工作正常,除了一..我在所有(簡單插入)中使用相同的查詢。我認爲它與我想上傳的文件有關,但我不確定。SQL查詢錯誤,試圖在數據庫中插入記錄

下面是代碼:

if ($_POST['action'] == 'hndlDocs') { 
$ref = $_POST['reference']; // Numeric value of 
$doc = file_get_contents($_FILES['doc']['tmp_name']); 


$link = mysqli_connect('localhost','XXXXX','XXXXX','documents'); 
mysqli_query($link,"SET autocommit = 0"); 

$query = "INSERT INTO documents ({$ref}, 
'{$doc}', 
'{$_FILES['doc']['type']}') 
;"; 
mysqli_query($link,$query); 
if (mysqli_error($link)) { 
    var_dump(mysqli_error($link)); 
    mysqli_rollback($link); 
} else { 
    print("<script> window.history.back(); </script>"); 
    mysqli_commit($link); 
} 

} 

的數據庫只有這些領域:

DATABASE documents (
    reference INT(5) NOT NULL, //it is unsigned zerofill 
    doc LONGBLOB NOT NULL, //this should contain the binary data 
    mime_type TEXT NOT NULL // the mime type of the file php allows only application/pdf and image/jpeg 
); 

而我得到的錯誤是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00001, '����' at line 1 

UPDATE:

除了我忘記在SQL查詢中添加VALUE。 我必須將addslashes()添加到從文件收集的內容中,並且全部完成。

我會感謝每一個幫助。 乾杯!

+1

哪裏是價值觀的文章? –

+0

啊...非常感謝你我沒有看到..謝謝你們和男人...我看起來很笨... –

回答

2

您忘記了添加VALUES關鍵字。您使用的INSERT語法的類型是隱含的。所以假設你的表只有3列,因爲你只提供了3個值。但是,如果您的列數超過3列,則需要在查詢中明確定義列名稱。

$query = "INSERT INTO documents VALUES ({$ref}, 
             '{$doc}', 
             '{$_FILES['doc']['type']}');"; 

查詢與SQL Injection脆弱。請花時間閱讀下面

+0

目前我正在建設使其運行的基本結構,並在此之後應用安全措施。無論如何,我會檢查文章。 乾杯! –