破损的文件图标从数据库中检索图像php和mysql

问题描述:

我需要从数据库上传和检索图像,我能够将图像存储在数据库中,但以后无法显示。请帮忙 我写了下面的代码从数据库中检索。破损的文件图标从数据库中检索图像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

它实际上是

只是一个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