0

I was trying to build form to to get product and this error came.

Notice: Undefined index: product_image in C:\xampp\htdocs\E-commerce\Admin_area\insert_product.php on line 159

Notice: Undefined index: product_image in C:\xampp\htdocs\E-commerce\Admin_area\insert_product.php on line 160 insert into products (product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keyword) values ('1','1','HP laptop','2000',' dasdasdda

','','asda')

PS: I know it's repeated question but I still can't solve it.

<!DOCTYPE>
<!DOCTYPE html>

<?php 

include("includes/db.php");

?>

<html>
<head>

    <title>Inserting Product</title>

    <script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
    <script>tinymce.init({ selector:'textarea' });</script>

</head>

<body bgcolor="skyblue">

    <form action="insert_product.php" method="post" enctype="multipart/formdata">

        <table align="center" width="700px" border="2px" bgcolor="Green">

            <tr align="center">

                <td colspan="7"><h2>Insert New Product</h2></td>

            </tr>

            <tr>

                <td align="center"><b>Product Title*</b></td>
                <td><input type="text" name="product_title" size="40" required=""></td>

            </tr>

            <tr>

                <td align="center"><b>Product Category</b></td>
                <td>

                <select name="product_cat" required="">

                    <option>Select 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="center"><b>Product Brand</b></td>
                <td>

                    <select name="product_brand" required="">

                    <option>Select Brand</option>

                    <?php 

                        $get_brand = "select * from brands";
                        $run_brand = mysqli_query($con, $get_brand);

                        while ($row_brand = mysqli_fetch_array($run_brand)) {

                            $brand_id = $row_brand['brand_id'];
                            $brand_title = $row_brand['brand_title'];

                            echo "<option value='$brand_id'>$brand_title</option>";

                            }

                     ?>

                </select>

                </td>

            </tr>

            <tr>

                <td align="center"><b>Product Image</b></td>
                <td><input type="file" name="product_image"></td>

            </tr>

            <tr>

                <td align="center"><b>Product Price</b></td>
                <td><input type="text" name="product_price" required=""></td>

            </tr>

            <tr>

                <td align="center"><b>Product Description</b></td>
                <td><textarea name="product_desc" cols="20" rows="10"></textarea> </td>

            </tr>

            <tr>

                <td align="center"><b>Product Keyword</b></td>
                <td><input type="text" name="product_keyword" size="40" required=""></td>

            </tr>

            <tr align="center">

                <td colspan="7"><input type="submit" name="insert_post" value="Insert Now" required=""></td>

            </tr>

        </table>


    </form>


</body>
</html>

<?php

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

        //Getting Text

        $product_title = $_POST['product_title'];
        $product_cat = $_POST['product_cat'];
        $product_brand = $_POST['product_brand'];
        $product_price = $_POST['product_price'];
        $product_desc = $_POST['product_desc'];
        $product_keyword = $_POST['product_keyword'];

        //Getting Image

        $product_image = $_FILES['product_image']['name'];
        $product_image_tmp = $_FILES['product_image']['tmp_name'];

        //Inserting Data

        echo $insert_product = "insert into products (product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keyword) values ('$product_cat','$product_brand','$product_title','$product_price','$product_desc','$product_image','$product_keyword')";

    }

 ?> 
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Where is that line 159 in your code? – arkascha Oct 08 '17 at 11:28
  • And btw: that is not an error, but a _notice_... – arkascha Oct 08 '17 at 11:29
  • Undefined index of... It means that `product_image` is not part of the array, either `$_POST` or `$_FILE`. So your first step into fixing this would be to see if you are actually posting the values before asking for them. – Sam Oct 08 '17 at 11:29
  • line 159 : $product_image = $_FILES['product_image']['name']; – Naman Shrivastava Oct 08 '17 at 11:37
  • @NamanShrivastava check my answer below it will help you – B. Desai Oct 08 '17 at 11:42
  • Okk then just update the table then ask to look at it....@Samuel – Naman Shrivastava Oct 08 '17 at 11:58
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – halfer Oct 08 '17 at 12:33
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 08 '17 at 12:34

1 Answers1

0

You have miss-spelled the enctype value in the form:

change

<form action="insert_product.php" method="post" enctype="multipart/formdata">

to

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