PHP图像没有出现

时间:2011-11-25 15:20:18

标签: php mysql error-handling

  

可能重复:
  Why isn't my image showing up?

我这里有一个奇怪的问题。我有这行代码,它在一个页面上工作,但它不在另一个页面上。 PHP代码如下:

显示图像的PHP页面

 <table border=1>
  <tr>
        <td align=center>EDIT</td>
      </tr>
<tr>
<td>
<table>
<?
$id = $_GET['product_id'];
$result = mysql_query("SELECT * FROM products WHERE serial = '$id'");
$info = mysql_fetch_array($result);
?>
<form method="post" action="editsuccess.php">
<input type="hidden" name="id" value="<? echo "$info[name]"?>">
<table border='0' width=100%>
<tr>       
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<? echo "$info[name]"?>">
</td>
</tr>

<tr>       
<td>Description</td>
<td>
<input type="text" name="name"
size="20" value="<? echo "$info[description]"?>">
</td>
</tr>
<tr>
<td>Price</td>
<td>
<input type="text" name="address" size="40"
value="<? echo "$info[price]"?>">
</td>
</tr>
<tr>       
<td>Image</td>
<td>
<? echo'<img src="../getImage.php?id=' . $info['serial'] .'"/>'?>
</td>
</tr>

<tr>
<td align="right">
<input type="submit"
name="submit value" value="Update Product">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>

不显示图像的PHP页面

 <?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/>
<span id="sprytextfield1">
<input type="text" value="<?php echo $name;?>" name="name"/>
<span class="textfieldRequiredMsg">Enter Product Name</span></span><br/>

Description:<br/>
<span id="sprytextfield2">
<input type="text" value="<?php echo $description;?>" name="description"/>
<span class="textfieldRequiredMsg">Enter A Description</span></span><br/>

Price:<br/>
<span id="sprytextfield3">
<input type="text" value="<?php echo $price;?>" name="price"/>
<span class="textfieldRequiredMsg">Enter Price</span><span class="textfieldInvalidFormatMsg">Enter Numbers Only</span></span><br/>

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

</br>

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

</form>

我正在谈论的代码是这一行:

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

为什么它不起作用的任何想法???

----- EDIT --------

getImage.php代码如下:

    <?php
$host="localhost"; // Host name
$user="****"; // Mysql username
$passwd="****"; // Mysql password
$dbName="**********"; // Database name

// Connect to server and select databse.
mysql_connect("$host", "$user", "$passwd")or die("cannot connect");
mysql_select_db("$dbName")or die("cannot select DB");


$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);
header("Content-type: image/jpeg");
echo $row['picture'];
?>

2 个答案:

答案 0 :(得分:0)

首先,尽量避免使用短标签(<?)。并非每个Web服务器都配置为理解它们,并且它与XML标记(使用<?xml打开)存在冲突。因此,请将<?替换为<?php,以确保您的代码始终适用于任何网络服务器,无论其是否为short_open_tags配置设置。

其次,您正在调用$row['serial'],但$row似乎不是一个数组(至少它没有在您粘贴的代码中定义)。你确定它不应该是$info['serial']吗?

但最重要的是,每当您允许用户输入(如$ _GET)来确定您的SQL查询时,始终使用mysql_real_escape_string转义您的代码,如下所示:

$result = mysql_query("SELECT * FROM products WHERE serial = '" . mysql_real_escape_string($id) . "'");

或者当您确定它始终是一个整数时(例如,如果该字段在数据库中具有INT数据类型),则将该值转换为整数,如下所示:

$result = mysql_query("SELECT * FROM products WHERE serial = " . (int) $id);

答案 1 :(得分:-1)

你赶紧去除cicluse

下一个代码     while($ info = mysql_fetch_array($ query)){     echo“”;

$name = $info['name'];
$description = $info['description'];
$price = $info['price'];
$picture = $info['picture'];
}
?>

仅使用

进行更改
$info = mysql_fetch_array($query)

尝试并告诉我们确实没问题,并且确实如您所愿。 :)