2015-04-23 43 views
3

我需要從數據庫上傳和檢索圖像,我能夠將圖像存儲在數據庫中,但以後無法顯示。請幫忙 我寫了下面的代碼從數據庫中檢索。破損的文件圖標從數據庫中檢索圖像php和mysql

$result1=mysql_query("INSERT INTO userdata(id, username, firstname, lastname, imageType, image)VALUES('', '" . $_SESSION['username'] . "', '" . $_SESSION['firstname'] . "', '$lastname','{$image_size['mime']}','{$imgData}')") or die("Invalid query: " . mysql_error()); 
if($result1) 
{ 
echo "</br>"; 
echo "Registration successful"; 
echo "</br>"; 
echo $lastid=mysql_insert_id();//get the id of the last record 
echo "uploaded image is :"; ?> 
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/> 

<?php 
echo "</br>";  
}#if result1into db successful 
else 
{ 
echo $result1; 
echo "Problem in database operation"; 

imageView.php具有下面的代碼:

<?php 
    $conn = mysql_connect("localhost", "root", ""); 
    mysql_select_db("wordgraphic") or die(mysql_error()); 
     if(isset($_GET['id'])) { 
     $sql = "SELECT imageType,image FROM userdata WHERE id=". $_GET['image_id']; 
     $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error()); 
     $row = mysql_fetch_array($result); 
     header("Content-type: " . $row["imageType"]); 
     echo $row["image"]; 
     } 
    mysql_close($conn); 
?> 

還有什麼可能是錯誤的代碼? 當我嘗試運行imageView.php與靜態ID圖像正在顯示。所以我猜錯誤是在傳遞變量是這樣的代碼:

echo "uploaded image is :"; ?> 
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/> 
<?php 

什麼可能是錯的?

+0

爲什麼使用'if(isset($ _GET ['id']))'而您不使用URI傳遞它? –

+0

它實際上是

回答

0

只是一個SMaL公司整改

<img src="imageView.php?image_id=<?php echo $lastid; ?>" /> 

,而不是在那裏UR保存圖像文件中的圖片文件夾讓說這 SRC = SRC路徑應該在此$imagedata店的文件名在DB這樣就可以了從圖像文件夾中檢索它

<img src="images/$imgData" width="your wish" height="your wish"/> 
+0

但我已經保存圖像在數據庫中,而不是在文件夾中,而不是「/> lastid是最後插入的圖像的ID,我希望dt圖像被顯示。 –

+0

而不是將它保存在你的數據庫中,這是一個最好的做法,在一個文件夾中保存文件上傳,然後將該文件名保存到你的數據庫,因爲我在src文件中添加該文件夾,然後添加你通過這種方式存儲在數據庫中的圖像名稱,這非常容易,即使通過簡單地使用unlink(folderpath/imagename)刪除該圖像, – Neelesh