doubingling4706 2013-06-09 21:57
浏览 76

使用php在数据库中插入图像[关闭]

I have problem with my code which is to insert image in the database using php the code worked great but when I connect it to the database I start to get an error.

upload_image.php

<html>
<head>
<title> Uploading Images</title>
</head>
<body>
</br>
</br>
</br>
<form style="text-align:center" action="upload_image.php"  method="post"   enctype="multipart/form-data">

Select Image: <input type="file"    name="image"  >
<input type="submit"    name="upload"  value ="Upload Now">       
</form>
</body>

</html>

<?php

mysql_connect("localhost","name","pssword")or die(mysql_errno());
mysql_select_db("dbName")or die(mysql_errno());

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

if(!isset($file))   

echo "please select an image";   

else 
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addcslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);

if($image_size == FALSE)
echo "this is not an image"; 
else
{
if (!$insert = mysql_query("insert into stores values('','$image_name',$image' )"))
echo "problem uploading image";
else 
{
{ 
$last_id = mysql_insert_id();
echo "image uploaded. <p/> your image:<p/><img src=get_image.php?id=$last_id>";
}   
}
}
}
?>

get_image.php

<?php
mysql_connect("localhost","name","pssword")or die(mysql_errno());
mysql_select_db("dbName")or die(mysql_errno());

$id = addslashes($_REQUEST['id']);
$image = mysql_query("select * from stores where id = $id");
$image= mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>

This is the error:

Notice: Undefined index: image in C:\wamp\www\upload_image.php on line 30
  • 写回答

1条回答 默认 最新

  • dongxi4335 2013-06-09 22:01
    关注
    $file=$_FILES['image']['tmp_name'];
    

    I believe the error is being generated here because, when it isn't set, the image index is undefined (kapow!)

    You could try this instead:

    if ( ! isset($_FILES['image']))
    {
        echo 'Please select a file..';
    }
    else
    {
        // your code
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测