注意:未定义索引:图像

时间:2014-05-08 08:36:33

标签: php mysql sql

我收到此错误提示:未定义索引:图像。我正在尝试将图像上传到数据库。我无法弄清楚发生了什么,其他一切都上传了除了图像,甚至图像名称都没有上传到数据库

下面是代码,文件大小只有3kb。

echo $max_upload = (int)(ini_get('upload_max_filesize')); 

echo "<pre>" . print_r($_POST, true) . "</pre>";

 echo "<pre>". var_dump($_FILES['image']) . "</pre>";

print_r显示为

 Array
(
[title] => This is a post
[author] => Me
[keywords] => posting
[image] => url.jpg
[content] => this is a new post 
[submit] => Publish now
)

将vardump显示为NULL

下面是实际代码。

  <html>

<head> 
<title> inserting new posts </title>
</head>

<body>


<form method="post" action="insert_post.php" enctype="multipart/form-data/">

<table width="600" align="centre" border"10">

<tr>
<td> <h1> Insert New Post here </h1> </td>
</tr>

<tr>

<td> Post title <td>
<td> <input type="text" name="title" size="30"> </td>
</tr>

<tr>
<td> Post Author<td>
<td> <input type="text" name="author" size="30"> </td>
</tr>

<tr>
<td> Post keywords<td>
<td> <input type="text" name="keywords" size="30"> </td>
</tr>

<tr>
<td> Post image <td>    
<td> <input type="file" name="image"> </td>
</tr>


<tr>
<td> Post Content <td>
<td> <textarea name="content" cols="20" rows="20" size="30"> </textarea> </td>
</tr>

<tr>
<td> Post title <td>
<td> <input type="text" name="title" size="30"> </td>
</tr>


<tr>
<td> <input type="submit" name="submit" value="Publish now"> </td>
</tr>

</table>

</form> 



</body>
</html>


<?php


include('../includes/connect.php');

if(isset($_POST['submit'])){


echo $post_title=$_POST['title'];

echo $post_date=date('d-m-y');

echo $post_author=$_POST['author'];

echo $post_keywords=$_POST['keywords'];

echo $post_content=$_POST['content'];

echo $post_image=$_FILES['image']['name'];

echo $image_tmp=$_FILES['image']['tmp_name'];




if($post_title=='' or $post_keywords=='' or $post_content=='' or $post_author==''){

echo "<script> alert('none of the fields can be empty')</script>";

exit();

}  else{

move_uploaded_file($image_tmp,"../images/$post_image");

    //query is below to insert data 

$insert_query="INSERT into posts
(post_title,post_date,post_author,post_image,post_keywords,post_content)
 VALUES('$post_title','$post_date','$post_author','$post_image','$post_keywords','$post_content')";



if(mysqli_query($connect,$insert_query)){

echo " <h1> successfully Posted </h1> ";


} else " h1> Did not work </h1>";




}








} 


 echo $max_upload = (int)(ini_get('upload_max_filesize')); 

echo "<pre>" . print_r($_POST, true) . "</pre>";

 echo "<pre>". var_dump($_FILES['image']) . "</pre>";

&GT;

1 个答案:

答案 0 :(得分:0)

enctype语法错误:

<form method="post" action="insert_post.php" enctype="multipart/form-data/">

这应该是

<form method="post" action="insert_post.php" enctype="multipart/form-data">

更多information