retrieve and displaying images in php using mysql

2019-10-18 14:44发布

问题:

Possible Duplicate:
retrieve and insert image in database using php

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-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 ?

回答1:

code goes like bellow Insert image/upload image

// Temporary file name stored on the server
$tmpName  = $_FILES['image']['tmp_name'];
$filename =  $_FILES["file"]["name"];
move_uploaded_file($tmpname, "upload/".$filename);

$query="INSERT INTO food (cat_id, food_name, food_price,image) VALUES($cat_id,$food_name,$foodprice, $filename);

Display image $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"];
    echo '<img src="upload/'.$food_name["image"].'" />';
}

That's all enjoy



回答2:

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');

Display image

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

Get image from 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;