2015-03-02 37 views
-2

我正在用php和mysql創建一個購物網站。我試圖上傳並保存圖像到MySQL數據庫。每當我上傳圖片時,它都不會存儲在數據庫中。我一直在想這幾天。當我運行這段代碼的圖像不保存並顯示一條錯誤消息說「錯誤在服務器上上傳圖片」如何在mysql數據庫上上傳圖片

PHP代碼

<?php 

include("common.php"); 

    function GetImageExtension($imagetype) 

    { 

     if(empty($imagetype)) return false; 

     switch($imagetype) 

     { 

      case 'image/bmp': return '.bmp'; 

      case 'image/gif': return '.gif'; 

      case 'image/jpeg': return '.jpg'; 

      case 'image/png': return '.png'; 

      default: return false; 

     } 

    } 

if (!empty($_FILES["uploadedimage"]["name"])) { 

    $file_name=$_FILES["uploadedimage"]["name"]; 

    $temp_name=$_FILES["uploadedimage"]["tmp_name"]; 

    $imgtype=$_FILES["uploadedimage"]["type"]; 

    $ext= GetImageExtension($imgtype); 

    $imagename=date("d-m-Y")."-".time().$ext; 

    $target_path = "images/".$imagename; 

if(move_uploaded_file($temp_name, $target_path)) { 

    $query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES 

('".$target_path."','".date("Y-m-d")."')"; 

mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); 

}else{ 
exit("Error While uploading image on the server"); 
} 
} 
?>; 

形式

<form action="testing.php" enctype="multipart/form-data" method="post"> 
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5"> 

<tbody><tr> 

<td> 

<input name="uploadedimage" type="file"> 

</td> 
</tr> 
<tr> 

<td> 

<input name="Upload Now" type="submit" value="Upload Image"> 

</td> 

</tr> 

</body></table> 

</form> 
+0

我會說,當他試圖做的錯誤是:'move_uploaded_file($ temp_name,$ target_path)'。您是否已將錯誤顯示打開,並將error_reporting設置爲E_ALL? – alfallouji 2015-03-02 19:43:04

+0

如果您無權寫入目標文件夾(本例爲images /),則會觸發錯誤並顯示錯誤消息。確保您擁有該文件夾的正確權限 – imelgrat 2015-03-02 19:45:37

回答

0

這個問題似乎在你的查詢中加上引號。

這樣做

$date = date("Y-m-d"); 
"INSERT into images_tbl (images_path,submission_date) VALUES ('{$target_path}', '{$date}')";