2017-04-09 68 views
0

嘿,我有以下代碼:uploade圖像MySQL數據庫失敗

index.php

select.php

,你也可以在GitHub上找到整個工程(簡單的在線圖書館)

它工作正常,並保存在mysql數據庫書名bookauthor書頁,但它只是保存圖像名稱和數據庫中的圖像路徑我只是想將圖像移動到新的文件夾稱爲圖像,並嘗試幾種方法,但沒有一個窩對我來說。

+0

首先您修復連接'$連接= mysqli_connect($ DB_HOST,$ db_username,$ DB_PASS,$ DB_NAME);' – Mario

+0

加上'的var_dump($文件);''之前$ query'也看到什麼信息你得到 – Mario

+0

@Mario謝謝你的回答,我修復了我的連接,並添加了var_dump,但是它什麼都沒顯示,只是瀏覽器提示框顯示[error。](http://i.imgur.com/elCHWPs.png )3線:'$文件=和addslashes(的file_get_contents($ _ FILES [ 「圖片」] [ 「tmp_name的值」));' – jackson

回答

1

應該是固定的,如下

<?php 
require_once("db.php"); 
$file = addslashes(file_get_contents($_FILES["images"]["tmp_name"])); 
$sql = "INSERT INTO test1(name, lastname, age , images) VALUES('".$_POST["name"]."', '".$_POST["lastname"]."', '".$_POST["age"]."', '".$file."')"; 
if(mysqli_query($connect, $sql)) 
{ 
    echo 'infos saved'; 
} 
?> 

UPDATE

您的AJAX腳本只發送文件名,而不是圖像。正確的JS應該與下面的類似。

$.ajax({ 
url: "ajax_php_file.php", // Url to which the request is send 
type: "POST",    // Type of request to be send, called as method 
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values) 
contentType: false,  // The content type used when sending data to the server. 
cache: false,    // To unable request pages to be cached 
processData:false,  // To send DOMDocument or non processed data file it is set to false 
success: function(data) // A function to be called if request succeeds 
{ 
    // do something ex. $('#loading').hide(); 
} 
}); 
+0

爲什麼要存儲臨時名稱?他以後如何獲得形象? – Mario

+0

@Mario使用的file_get_contents讀取文件的內容,它應該工作 –

+0

@Оzgur感謝答案,我用旅遊代碼和更新我的,但仍然沒有改變,沒有什麼上傳圖片排DB它只是顯示在瀏覽器的警告框這個[錯誤。 ](http://i.imgur.com/elCHWPs.png)3行:'$文件=和addslashes(的file_get_contents($ _ FILES [ 「圖像」] [ 「tmp_name的值」]));' – jackson