3

我需要从数据库上传和检索图像,我可以将图像存储在数据库中,但以后无法显示。请帮助我编写了以下代码以从数据库中检索。

  $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

什么可能是错的?

4

1 回答 1

0

只是一个小整改

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

而不是这个 src = src 路径,您在其中保存图像文件可以说在图像文件夹中,该文件夹将在这里$imagedata将文件名存储在数据库中,以便您可以从图像文件夹中检索它

<img src="images/$imgData" width="your wish" height="your wish"/>
于 2015-04-23T12:19:45.250 回答