破坏的文件图标从数据库php和mysql检索图像 [英] broken file icon retrieving image from database php and mysql
问题描述
我需要从数据库上传和检索图片,我可以将图片存储在数据库中,但无法在以后显示。请帮助
我写了以下代码从数据库中检索。
I need to upload and retrieve image from a database, I am able to store the image in the database but unable to display it later. please help I wrote the following code to retrieve from the database.
$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具有以下代码:
imageView.php has the following code:
<?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图像正在显示。所以我猜错误传递的变量是这个代码:
What could be possibly wrong with the code?
When i try to run imageView.php
with static id image is getting displayed. So i guess the error is in passing the variable is this code:
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>
<?php
什么可能会出错?
推荐答案
只是一个小修正
<img src="imageView.php?image_id=<?php echo $lastid; ?>" />
而不是这个
src = src path保存图像文件文件夹,这将是这里 $ imagedata
将文件名存储在DB中,以便您可以从图像文件夹中检索
instead of this
src = src path where u r saving the image file lets say in images folder that would be here $imagedata
store the file name in the DB so that you can retreive it from the image folder
<img src="images/$imgData" width="your wish" height="your wish"/>
这篇关于破坏的文件图标从数据库php和mysql检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!