-4

I'm making an edit page of category list. Everything is fine but i'm not able to edit the uploaded file.

The error is:

Notice: Undefined index: fileToUpload in E:\xampp\htdocs\chandra\Categories\New cat\edit1.php on line 15

 <?php
    include("config.php"); //making database conn
    if(isset($_GET['id'])) //if condition for getting id 
    {

    $id=$_GET['id']; //storing result in a variable 

    //if we press update button - START 
     if(isset($_POST['update'])) 
      {

        $parent =  $_POST['parent']; //getting  value through post method
        $child  =  $_POST['child'];
        $status=$_POST['Status'];  
        $target_file  = $_POST['fileToUpload'];
        // update query
        $query3=mysqli_query($conn,"update new_cat1 set 
                                            p_id='$parent', 
                                            name='$child' ,
                                            Status='$status',
                                            pic='$target_file'
                                    where id=$id ");
            // if query succes then redirect our page                   
             if($query3)
              {
              $msg="Successfully Updated!!";
             // header('Location:fetch.php');
             echo $query3;
              }                     


      }
      //IF end == if we press update button - ENDS 

         //select query 
        $query1=mysqli_query($conn,"select * from new_cat1 where id=$id");
        //echo "select * from new_cat1 where id='$id'";
        // while loop when query succes then store the result into variable
            while($query2 = mysqli_fetch_assoc($query1)) {

             $parent = $query2["p_id"];
                $child = $query2["name"]; 
                $status=$query2["Status"];
                $target_file  = $query2["pic"];
            }


    ?>

    <form name="update" action="" method="post" enctype="multipart/form-data">

      <?php
      //select query for parent category
    $select_query=          "Select * from new_cat1 where p_id=0";
    $select_query_run =     mysqli_query($conn,$select_query);

    echo "<select name='parent'>";
    echo "<option>---select---</option>";   
    while ($select_query_array=   mysqli_fetch_array($select_query_run) )
    {

            $id = $select_query_array["id"];
            $name = $select_query_array["name"]
            // for showing the option statement selected

       ?>
       <option value="<?=$id?>" <?php if($parent==$id) echo "selected='selected'"; ?> ><?=$name?></option>

       <?php

    }
    echo "</select>";
    // echo $id.$parent;
     ?><!-- input feild -->
      <input type="text" name="child" value="<?=$child?>">
       Active:<input type="radio" value="Active" name="Status" <?php if($status=="Active") echo 'checked="checked"'; ?>>
      Inactive:<input type="radio" value="Inactive" name="Status" <?php if($status=="Inactive") echo 'checked="checked"'; ?>>
               <input type="file" name="fileToUpload" value="<?=$target_file?>"/>
      <input type="submit" value="update" name="update">

    </form>
    </body></html>
    <?php
    } // if closed
    ?>
rpy
  • 3,953
  • 2
  • 20
  • 31
DHYAN
  • 1
  • 2
  • 2
    For file type use `$_FILES['fileToUpload']['name']` to get file name – Saty Jun 06 '16 at 06:15
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Gerald Schneider Jun 06 '16 at 06:17

1 Answers1

0

As $_FILES contains associative array of items uploaded to the current script via the HTTP POST method.

Change $_POST['fileToUpload'] to $_FILES['fileToUpload']

Use $_FILES['fileToUpload']['name'] to store file name into database

Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
  • after changing $_POST to $_FILES showing this error :Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in E:\xampp\htdocs\chandra\Categories\New cat\edit1.php on line 39 – DHYAN Jun 06 '16 at 06:23
  • Use $_FILES['fileToUpload']['name'] to store into databse – Dhara Parmar Jun 06 '16 at 06:24