2012-07-19 171 views
-3

可能重复在PHP中显示图像:
retrieve and insert image in database using php检索和用mysql

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

1-我的第一项任务是显示点击类别的食物项目。有代码和它正在完美运作

$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());} 
} 

说明如何插入图像并将它们与其他数据一起显示?

+3

第三次,你都张贴了这个问题。 – 2012-07-19 06:56:07

回答

0

代码都像波纹管 插入图像/上传图片

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

显示图像 $查询= “从食物中选择*其中CAT_ID = {$ _ GET [ '类']}”;

$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"].'" />'; 
} 

这一切享受

+0

此代码中的提示错误 echo''; – user1533946 2012-07-19 07:12:53

+0

它必须是语法,至少你可以管理:) 或者你可以解释是什么错误 – 2012-07-19 07:15:27

+0

$ 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); runnin此部分它给出下面的错误您有错误你的SQL语法;检查对应于你的MySQL服务器版本的手册,在第1行',,)附近使用正确的语法 – user1533946 2012-07-19 07:18:52

0

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

$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;