dqz7636 2012-07-19 06:55
浏览 17

使用mysql在php中检索和显示图像[复制]

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 ?

  • 写回答

2条回答 默认 最新

  • doutao6330 2012-07-19 07:05
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了