-5

I get Undefined index: image error in this code. Can I know the exact solution? I wanna know workflow from line abc to xyz that I commented in the code using //.Thanks for your help..

   <?php session_start();
  include("config.php");
 if(isset($_SESSION['name']))
 {
if(!$_SESSION['name']=='admin')
{
header("Location:login.php?id=You are not authorised to access this page unless you are administrator of this website");
}
}
?>
 <?php
  $name=$_FILES['image']['name'];
 $tmp=$_FILES['image']['tmp_name'];
  $err=$_FILES['image']['error'];
   }
 if($err==0)
 {
 move_uploaded_file($tmp, $name);
  //xyz}
 $category=$_POST['category'];
 $title=$_POST['title'];
$image=$_FILES["image"]["name"];
$content=$_POST['content'];
}
<?php
 $qry=mysql_query("INSERT INTO articles(title,image,content,category)VALUES('$title','$image','$content','$category')");
if(!$qry)
 {
die("Query Failed: ". mysql_error());
}
else
{
echo "Article Added Successfully";
}
 ?>

  The form code is here:
<?php
include("config.php");
$sql=mysql_query("select * from category");
if(!$sql)
{
mysql_error();  
}
?>
<form action="created_article.php" method="post">
Category:
<select name="category">
<?php
while($row=mysql_fetch_array($sql))
{
echo"<option value='".$row['category']."'>".$row['category']."</option>";
}
?>
</select>
Title:
<input type="text" name="title"/>
Upload Image:
<input type="file" name="image" id="image" />
Contents:
<textarea name="content" cols="100" rows="12" ></textarea>
<input type="submit" name="button"  value="Submit" />
</form>

I need help with these code, I need to make project and I'm stuck here, so please kindly I request for your help,

2 Answers2

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

define enctype in your form tag otherwise its not work for image

jilesh
  • 436
  • 1
  • 3
  • 13
0

While Dealing with input File always put form attribute enctype='multipart/form-data'

<?php

session_start ();
include ("config.php");
if (isset ( $_SESSION ['name'] )) {
    if (! $_SESSION ['name'] == 'admin') {
        header ( "Location:login.php?id=You are not authorised to access this page unless you are administrator of this website" );
    }
}
if (!empty($_POST))
{
    $name = $_FILES ['image'] ['name'];
    $tmp = $_FILES ['image'] ['tmp_name'];
    $err = $_FILES ['image'] ['error'];


    if($err==0) {
        move_uploaded_file($tmp, $name);

        $category=$_POST['category'];
        $title=$_POST['title'];
        $image=$_FILES["image"]["name"];
        $content=$_POST['content'];
    }

    $qry=mysql_query("INSERT INTO articles(title,image,content,category)VALUES('$title','$image','$content','$category')");

    if(!$qry){
        die("Query Failed: ". mysql_error());

    } else {
       echo "Article Added Successfully";

    }

 }
    include("config.php");
    $sql=mysql_query("select * from category");

    if(!$sql){

        mysql_error();  
    }
?>
The form code is here:
<form action="created_article.php" enctype='multipart/form-data' method="post">
    Category: <select name="category">
<?php
while($row=mysql_fetch_array($sql))
{
echo"<option value='".$row['category']."'>".$row['category']."</option>";
}
?>
</select> Title: <input type="text" name="title" /> Upload Image: <input
        type="file" name="image" id="image" /> Contents:
    <textarea name="content" cols="100" rows="12"></textarea>
    <input type="submit" name="button" value="Submit" />
</form>
kamlesh.bar
  • 1,774
  • 19
  • 38
  • that didn't work .. the error undefined index on image still persists on this line.. $name=$_FILES['image']['name']; $tmp=$_FILES['image']['tmp_name']; $err=$_FILES['image']['error']; $image=$_FILES["image"]["name"]; I have added "enctype="multipart/form-data" to form ..but still didn't work. I have added if (!empty($_POST)) too.. please sort this out.. –  Nischal Prajapati Jan 05 '16 at 22:32
  • have you uploaded file from form ? – kamlesh.bar Jan 06 '16 at 05:40