从MySQL数据库中检索和显示图像(带条件)

时间:2014-04-21 16:46:37

标签: php mysql image imageview

我在开发网站时遇到了问题..

首先,我有两张桌子:

1- 产品:包含信息。关于产品和2- 图像:包含产品图像。

我的想法是:我想为指定的产品插入一两个图像,并仅为该产品显示该图像。

同时,我可以将图像插入到数据库中,我也可以显示它们..但我的问题是,当我尝试显示时...代码从图像检索所有图像表..

所以我的问题:如何只显示一个产品的图片?而不是该表中的所有图片?我对如何使这种情况感到困惑..需要你帮助的人:?

这是我的代码:

File1:imageUpload.php

<?php

if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("myDB");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);

$sql = "INSERT INTO Images(imageType ,imageData, product_id)
    VALUES('{$imageProperties['mime']}', '{$imgData}','{$_POST['memids']}')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" .        

    mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}
 }
}
?>


<HTML>
<HEAD>
<TITLE>Upload Image</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post"   
class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" required/>
<input type="submit" name="submit" value="Submit" class="btnSubmit" /><br><br>

</form>
</div>

</BODY>
</HTML>

File2:listImages.php

<?ob_start()?>

<?php


$conn = mysql_connect("localhost", "root", "");
mysql_select_db("myDB");
$sql = "SELECT imageId FROM Images
      ORDER BY imageId DESC"; 
$result = mysql_query($sql);
?>


<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysql_fetch_array($result)) {
?>
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?> "width="200" /><br/>
<?php       
}
mysql_close($conn);
?>

</BODY>
</HTML>
<?ob_flush()?>

File3:imageView.php

<?php

$conn = mysql_connect("localhost", "root", "");
mysql_select_db("myDB") or die(mysql_error());
if(isset($_GET['image_id'])) {
    $sql = "SELECT imageType,imageData FROM Images WHERE imageId=" . $_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["imageData"];
}
mysql_close($conn);
?>

0 个答案:

没有答案