使用mysql在php中检索和显示图像 [英] retrieve and displaying images in php using mysql

查看:205
本文介绍了使用mysql在php中检索和显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用php检索并在数据库中插入图像

我正在插入和显示图像的各种数据。我正在一个restyrant网站上工作,我正在存储和显示项目及其图像。我创建了两个名为food的表(id [PK],名称,价格,image,cat_id [FK])类别(cat_id [PK],cat_name)1 - 我的任务是显示导航栏中的所有类别。 2 - 当我点击类别时,它应显示属于类别3插入的食物并更新信息

I am inserting and displaying various data with images.I am working on a restyrant website where I am storing and displaying the items along with their images.I have created two tables named food(id[P.K],name,price,image,cat_id[F.K]) category(cat_id[P.K],cat_name) 1-my task is to display the all categories in navigation bar. 2-when I click on the category it should display the food that comes under the category 3-insert and update the information

1 - 我的第一个任务是显示食物项目点击类别。这是代码,它工作正常

1-My first task was to display the food items onclicking the category.here is the code and it is working perfectly

 $category=mysql_query("Select * from category",$connection);

while($category_name=mysql_fetch_array($category)) 

{

$category_name['cat_name'];

echo  "<li><br><a href=\"show.php?category=

". urlencode($category_name["cat_id"]) . "\">

{$category_name["cat_name"]}</a></li>". "<br />";   
}


if(isset($_GET['category'])) 
{


$query="Select * from food where cat_id={$_GET['category']} "; 

$result=mysql_query($query,$connection); 

while($food_name=mysql_fetch_array($result)) 

{        

echo $food_name["food_name"]. " Price is " . $food_name["food_price"];

here i have to use the code inorder to display the images
}


here is code of inserting the data and images


this is the html form inserting fields in the database


<form enctype="multipart/form-data" method="post"  action="insert.php" >

<p>Name:<input type="text" name="food_name"  id="food_name"/>

<p>Price:<input type="text" name="food_price"  id="food_price"/>

<input name="MAX_FILE_SIZE" value="102400" type="hidden">

<input name="image" accept="image/jpeg" type="file">


this is the code of of selecting images


if (isset($_FILES['image']) && $_FILES['image']['size'] > 0 && $_POST['cat_name']) 

{ 

// Temporary file name stored on the server

$tmpName  = $_FILES['image']['tmp_name'];  

// Read the file 
$fp   = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);




$cat_id=$_POST['cat_name'];

$food_name=$_POST['food_name']

$foodPrice=$_POST['food_price'];

$query="INSERT INTO food (cat_id, food_name, food_price,image) VALUES
($cat_id,'$food_name','$foodPrice','$data')";


if(mysql_query($query,$con))
{
echo "New Item Added in the DataBase and picture has been uploaded";
}
else

{ die(mysql_error());} 
}

告诉如何插入图像并将其与其他数据一起显示?

tell how to insert the images and show them along with other data ?

推荐答案

暂时存储临时文件夹,读取拇指文件并存储在数据库中然后取消链接拇指图像
存储在数据库中的图像。

Temporarily stored temp folder,read thumb file and stored in db then unlink thumb image Image stored in DB.

$tname = "./temp/thumb";
$filet = fopen($tname,"rb");
$thumbData = addslashes(fread($filet,filesize($tname)));

insert into image (img_id,img_data) values('','$thumbData');

显示图片

<img src="image.php?img_id=1">

从DB获取图像

$id=$_GET['id'];
   $sql="select img_data from image where img_id=$id";
   $rs=mysql_query($sql) or die (mysql_error());
   $row =mysql_fetch_array($rs,MYSQL_BOTH);
   $data = $row[0];
   print $data;

这篇关于使用mysql在php中检索和显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆