alexa16 Posted May 8, 2014 Share Posted May 8, 2014 i am getting this error Notice: Undefined index: image . i am trying to upload an image to databse . I cannot figure out whats going on , everything else uploads except the image, not even the image name is uploaded to database below is code the size of the file is only 3kb. the echo $max_upload = (int)(ini_get('upload_max_filesize')); echo "<pre>" . print_r($_POST, true) . "</pre>"; echo "<pre>". var_dump($_FILES['image']) . "</pre>"; the print_r displays as Array ([title] => This is a post[author] => Me[keywords] => posting[image] => url.jpg[content] => this is a new post [submit] => Publish now) displays as NULL the vardump below is actual code. <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>"; ?> thanks in advance guys. Link to comment https://forums.phpfreaks.com/topic/288333-notice-undefined-index-image/ Share on other sites More sharing options...
PravinS Posted May 8, 2014 Share Posted May 8, 2014 you can use isset() function to check if $_FILES['image'] is set of not like if (isset($_FILES['image'])) { // your code here var_dump($_FILES['image']); // your code here } may this will help you Link to comment https://forums.phpfreaks.com/topic/288333-notice-undefined-index-image/#findComment-1478704 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.