2011-11-18 140 views
-2

-------编辑-------
嗨,大家好,看到你为我解决了这个问题,我认为这将是一个好主意,再次解决同样的问题,但在不同的页面。我无法看到图像。为什么我的图像不显示?

<?php 
$id = $_GET['product_id']; 
$query = mysql_query("SELECT * FROM products WHERE serial = '$id'") 
or die(mysql_error()); 

while($info = mysql_fetch_array($query)) { 
echo ""; 

$name = $info['name']; 
$description = $info['description']; 
$price = $info['price']; 
$picture = $info['picture']; 

} 
?> 

<form action="editsuccess.php?product_id=<?php echo $id; ?>" method="post"> 

Product ID:<br/> 
<input type="text" value="<?php echo $id;?>" name="product_id" disabled/> 

<br/> 

Name:<br/> 
<input type="text" value="<?php echo $name;?>" name="name"/> 

<br/> 

Description:<br/> 
<input type="text" value="<?php echo $description;?>" name="description"/> 

<br/> 

Price:<br/> 
<input type="text" value="<?php echo $price;?>" name="price"/> 

<br/> 

Picture:<br/> 
<? echo'<img src="../getImage.php?id=' . $info['serial'] .'"/>'?> 

</br> 

<input type="submit" value="Update Product"/> 

</form> 

这是一个页面,管理员可以从一个表中的一行编辑产品。 由于某种原因,图像没有显示出来。

感谢您的任何建议。

------编辑在这里结束--------

我还是不能让我的PHP图像即使在之后的图像上传到数据库中正确的方法后才会显示。下面的代码是用于显示图像:

<form name="form1"> 
    <input type="hidden" name="productid" /> 
    <input type="hidden" name="command" /> 
</form> 

    <table border="0" cellpadding="2px" width="600px"> 
     <? 
      $result=mysql_query("select * from products"); 
      while($row=mysql_fetch_array($result)){ 

     ?> 
     <tr> 
      <td><?php '<img src="getImage.php?id=' . $row['serial'] .'"/>' 
?> 

      </td> 
      <td> <b><a href="products.php?product_id=<?=$row['serial']?>"><?=$row['name']?></a></b><br /> 
        <?=$row['description']?><br /> 
        Price:<big style="color:green"> 
         £<?=$row['price']?></big><br /><br /> 
        <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" /> 
      </td> 
     </tr> 
     <tr><td colspan="2"><hr size="1" /></td> 
     <? } ?> 
    </table> 

getImage.php看起来是这样的:

... 
$link = mysql_connect($host, $user, $passwd); 
mysql_select_db($dbName); 
$query = 'SELECT picture FROM products WHERE serial="' . $_GET['id'] . '"'; 
$result = mysql_query($query,$link); 
$row = mysql_fetch_assoc($result); 
echo $row['picture']; 
?> 

只有名称,描述和价格显示在网页上了。我的MySQL表看起来像这样:

  • 串行
  • 描述
  • 价格
  • 图片(BLOB)
+1

是作为Blob存储的图像?还应该在输出图像时设置标题,另外您应该修复sql注入。 –

+0

是的,它被存储为一个blob ...你是什么意思设置标题? – Jahed

回答

3

你是不是呼应了之前设置正确Content-type头图像数据。

您还必须跳过$_GET['id']参数。

// Escape $id 
$id = mysql_real_escape_string($_GET['id']); 

$link = mysql_connect($host, $user, $passwd); 
mysql_select_db($dbName); 

// Use the escaped $id 
$query = "SELECT picture FROM products WHERE serial='$id'"; 
$result = mysql_query($query,$link); 

if ($result) { 
    $row = mysql_fetch_assoc($result); 

    // Set the Content-type 
    // This assumes image/jpeg. If you have different image types, 
    // you'll need logic to supply the correct MIME type 
    // image/jpeg image/png image/gif, etc 
    header("Content-type: image/jpeg"); 
    echo $row['picture']; 
} 
?> 

在主脚本,它看起来像你只是缺少一个echo

 <td><?php '<img src="getImage.php?id=' . $row['serial'] .'"/>' 
     // Should be 
     <td><?php echo '<img src="getImage.php?id=' . $row['serial'] .'"/>' 
     // ------^^^^^^ 
+0

嗨,我现在getImage.php页面上说错误:图片“http://www.*****.com/musics/getImage.php”无法显示,因为它包含错误。 – Jahed

+0

好吧,看起来你没有提供'id'。浏览到'http://www.yourdomain.com/musics/getImage.php?id = somevalidid' –

+3

这将表明1)没有为您的查询返回记录,2)表中的数据已损坏,3)您为该图像设置了错误的内容类型标题,或者4)您的脚本正在输出文本以及图像的数据,从而导致浏览器认为图像已损坏。 – Crontab