0

I create this upload form for uploading products to phpmyadmin but when I click on upload button its error in 4 lines:

Notice: Undefined index: file in C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 102
Notice: Undefined index: file in     C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 103
Notice: Undefined index: file in     C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 104
Notice: Undefined index: file in     C:\xampp\htdocs\MyShop\admin_area\insert_product.php on line 105

My html is:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            Untitled Document
        </title>
    </head>
    <body>
        <form action="insert_product.php" method="post" encypt="multipart/form-data">
            <table align="center" width="800" >
                <tr>
                    <td>
                        Insert New Post
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product title:
                        </b>
                    </td>
                    <td>
                        <input type="text" name="product_title" required/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product Category:
                        </b>
                    </td>
                    <td>
                        <select name="product_cat">
                            <option>Select a Category</option>
                            <?php
                            $get_cats = "select * from categories";
                            $run_cats = mysqli_query($con, $get_cats);
                            while ($row_cats=mysqli_fetch_array($run_cats)) {
                                $cat_id = $row_cats['cat_id'];
                                $cat_title = $row_cats['cat_title'];
                                echo "<option value='$cat_id'>$cat_title</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <b>
                            Product Image
                        </b>
                    </td>
                    <td>
                        <input type="file" name="file" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product Price:
                        </b>
                    </td>
                    <td>
                        <input type="text" name="product_price" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product Description:
                        </b>
                    </td>
                    <td>
                        <textarea name="product_desc"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>
                            Product keywords:
                        </b>
                    </td>
                    <td>
                        <input type="text" name="product_keywords" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" name="insert_post" value="Insert Product" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
<?php
if(isset($_POST['insert_post'])) {
    $product_title = $_POST['product_title'];
    $product_cat = $_POST['product_cat'];
    $product_price = $_POST['product_price'];
    $product_desc = $_POST['product_desc'];
    $product_keywords = $_POST['product_keywords'];
    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder="uploads/";
    // new file size in KB
    $new_size = $file_size/1024; 
    $new_file_name = strtolower($file);
    // make file name in lower case
    $final_file=str_replace(' ','-',$new_file_name); 
    // new file size in KB
    move_uploaded_file($file_loc,$folder.$final_file);
    $insert_product = "insert into products (product_cat,product_title,file,type,size,,product_price,product_desc,product_keywords) values ('$product_cat','$product_title','$final_file','$file_type','$new_size','$product_price','$product_desc','$product_keywords')";
    $insert_pro = mysqli_query($con, $insert_product);
    if($insert_pro) {
        echo "<script>alert('Product has been inserted!')</script>";
        echo "<script>window.open('insert_product.php','_self')</script>>";
    }
}
?>
arghtype
  • 4,376
  • 11
  • 45
  • 60
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – u_mulder Feb 04 '17 at 07:14
  • `encypt="multipart/form-data"` Okay. Now read manual again and see how to write this attribute properly. – u_mulder Feb 04 '17 at 07:15

2 Answers2

0

You need to check isset condition before taking action on $_FILES

 if(isset($_FILES['file']['name']))
{
 $file = rand(1000,100000)."-".$_FILES['file']['name'];
     $file_loc = $_FILES['file']['tmp_name'];
     $file_size = $_FILES['file']['size'];
     $file_type = $_FILES['file']['type'];
     $folder="uploads/";

     // new file size in KB
     $new_size = $file_size/1024; 
     $new_file_name = strtolower($file);
     // make file name in lower case

     $final_file=str_replace(' ','-',$new_file_name); 
     // new file size in KB

     move_uploaded_file($file_loc,$folder.$final_file);
 }
Pramod Patil
  • 2,704
  • 2
  • 14
  • 20
0

You have typo error in form enctype:

<form action="insert_product.php" method="post" enctype="multipart/form-data">
B. Desai
  • 16,414
  • 5
  • 26
  • 47