未定义索引:image和getimagesize:读取错误!在php中

未定义索引:image和getimagesize:读取错误!在php中,php,Php,这是我上传图片到数据库的代码 index.php: <html> <head> <title>upload images to database</title> </head> <body> <form action="index.php" method = "POST" enctype = "multipart/form-data"> File: <input type= "file" name= "im

这是我上传图片到数据库的代码

index.php:

<html>
<head>
<title>upload images to database</title>
</head>
<body>
<form action="index.php" method = "POST" enctype = "multipart/form-data">
File:
<input type= "file" name= "image">
<input type= "submit" value= "upload">
</form>
<?php
//connect to database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image") or die(mysql_error());
$file = '';
 $file= $_FILES['image']['tmp_name'];
 if(!isset($file))
 {
 echo "please select an image";
 }
 else
 {
$image = file_get_contents($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size ==FALSE)
{
echo "That's not an image";
}
else
    {
    if(!$insert = "INSERT INTO upload VALUES('','$image_name','$image')")
    {
    echo "Problem uploading image";
    }
    else{

    $lastid = mysql_insert_id();
    echo "Image upload.<p/>Your Image</p><img src=get.php?id=$lastid>";

    }
} 
 }

?>
</body>
</html>
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image") or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM upload WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("content-type: image/ipeg");
echo $image;
?>

将图像上载到数据库
文件:

您需要测试脚本是在表单刚开始显示时运行,还是在表单提交时运行:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    ... all the code that processes the form
}
?>
这对于
$image
尤其重要,因为它是二进制数据

您缺少调用
mysql\u query()
来执行
INSERT
。应该是:

if(!$insert = mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))
{
    echo "Problem uploading image: ".mysql_error();
}
else{

    $lastid = mysql_insert_id();
    echo "Image upload.<p/>Your Image</p><img src=get.php?id=$lastid>";

}
if(!$insert=mysql\u查询(“插入到上传值(“'$image\u name','$image')”)中)
{
echo“上传图像时出现问题:.mysql_error();
}
否则{
$lastid=mysql\u insert\u id();
echo“图像上传。

您的图像

”; }

如果切换到PDO或mysqli扩展,并使用准备好的查询,会更好。mysql扩展已被弃用。

首先,它是
INSERT
而不是
INESRT
。如果你的代码有误,请在你的问题中更改。@Fred ii-:是的,我更改了。。抱歉英语不好。您没有检查表单是否已提交;您没有检查
$\u文件['image']['error']
的任何错误状态;您的查询和mimetype标题中有输入错误
addslashes()
不是转义SQL输入的方法,您使用的是一个不推荐使用的接口(
mysql*()
)。可能还有其他问题。@MikeW:您能编辑我的代码吗?如何检查$\u文件['image']['error']?请不要删除原始问题。如果您想发布新代码供我们查看,请将其附加到问题中。阅读问题的人必须能够看到我们修复了什么。我已经回答了。如果您正确地转义了数据,我认为
INSERT
会起作用。您永远不会在
INSERT
查询中调用
mysql\u query
!因为
Save As
从链接中的URL获取默认文件名,并且图像的URL是
get.php?id=XXX
。您可以在服务器上使用重写规则将类似
/images/image\u XXX.jpg
的内容重写为
get.php?id=XXX
。然后使用链接中的第一个URL。
if(!$insert = mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))
{
    echo "Problem uploading image: ".mysql_error();
}
else{

    $lastid = mysql_insert_id();
    echo "Image upload.<p/>Your Image</p><img src=get.php?id=$lastid>";

}