我正在插入和显示图像的各种数据。我正在一个restyrant网站上工作,我正在存储和显示项目及其图像。我创建了两个名为food的表(id [PK],名称,价格, image,cat_id [FK])category(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());}
}
告诉如何插入图像并将其与其他数据一起显示?
答案 0 :(得分: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);
显示图片 $ query =“从食物中选择*,其中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"].'" />';
}
这一切都很享受
答案 1 :(得分:0)
暂时存储临时文件夹,读取拇指文件并存储在db中然后取消链接拇指图像 存储在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;