-2

I am making a insertion form to add a product to the mysql database. I am using echo command to print the query. The printed query is showing empty values like this:

Notice: Undefined index: product_cat in F:\Apache24\htdocs\Site1\admin\product_add.php on line 40

Notice: Undefined index: product_brand in F:\Apache24\htdocs\Site1\admin\product_add.php on line 41

Notice: Undefined index: product_title in F:\Apache24\htdocs\Site1\admin\product_add.php on line 42

Notice: Undefined index: product_price in F:\Apache24\htdocs\Site1\admin\product_add.php on line 43

Notice: Undefined index: product_description in F:\Apache24\htdocs\Site1\admin\product_add.php on line 44

Notice: Undefined index: product_keywords in F:\Apache24\htdocs\Site1\admin\product_add.php on line 45

Notice: Undefined index: product_images in F:\Apache24\htdocs\Site1\admin\product_add.php on line 48

Notice: Undefined index: product_images in F:\Apache24\htdocs\Site1\admin\product_add.php on line 49

insert into products ('product_cat', 'product_brand', 'product_title', 'product_price', 'product_desc', 'product_image', 'product_keywords') values( '', '', '', '', '', '', '')

This is the html form. It is also the action page:

<!DOCTYPE html>
<html>
<head>
    <title>Admin Panel - Add Products</title>
    <link rel="stylesheet" type="text/css" href="../styles/admin-style.css">
    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>tinymce.init({ selector:'textarea' });</script>

    <?php 
    include("../includes/connect.php");
    function getcats_add_products(){
        global $con;
        $get_cats="select * from categories";
        $run_cats=mysqli_query($con, $get_cats);                   
        echo "<option>Select Category</option>";

        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>";
        }
    }

     function getbrands_add_products(){
        global $con;
        $get_brands="select * from brands";
        $run_brands=mysqli_query($con, $get_brands);                   
        echo "<option>Select Brand</option>";

        while($row_brands = mysqli_fetch_array($run_brands)){
            $brand_id = $row_brands['brand_id'];
            $brand_title = $row_brands['brand_title'];
            echo "<option value='$brand_id'>$brand_title</option>";
        }
    }


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

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


    $product_images = $_FILES['product_images']['name'];
    $product_images_temp = $_FILES['product_images']['tmp_name'];

    $product_query = "insert into products
    ('product_cat',
    'product_brand',
    'product_title',
    'product_price',
    'product_desc',
    'product_image',
    'product_keywords') 
    values(
    '$product_cat',
    '$product_brand',
    '$product_title',
    '$product_price',
    '$product_desc',
    '$product_images',
    '$product_keywords') ";
    echo $product_query;
    }

    ?>

</head>
<body>
    <div class="wrapper">
    <header>

    </header>
    <div class="heading">Add New Product</div>
    <div class="product-table-div">
        <form method="POST" action="" enctype="multipart/form-data">
            <table class="product-table" border="1">
                <tr>
                    <td id="product-add-label">Product Category</td>
                    <td>
                        <select id="product-table-input" name="product-cat">
                            <?php getcats_add_products(); ?>
                        </select>
                    </td>
                </tr>    
                <tr>    
                    <td id="product-add-label">Product Brand</td>
                    <td>
                        <select id="product-table-input" name="product-brand">
                            <?php getbrands_add_products(); ?>
                        </select>
                    </td>
                </tr>    
                <tr>
                    <td id="product-add-label">Product title</td>
                    <td>
                        <input type="text" name="product-title" id="product-table-input">
                    </td>
                </tr>    
                <tr>
                    <td id="product-add-label">Product Price</td>
                    <td>
                        <input type="number" name="product-price" id="product-table-input">
                    </td>
                </tr>    
                <tr>
                    <td id="product-add-label">Product description</td>
                    <td>
                        <textarea rows="10" cols="30" name="product-description"></textarea>
                    </td>
                </tr>    
                <tr>
                    <td id="product-add-label">Product image</td>
                    <td>
                        <input type="file" name="product-images" id="product-table-input">
                    </td>
                </tr>    
                <tr>
                    <td id="product-add-label">Product Keywords</td>
                    <td>
                        <input type="text" name="product-keywords" id="product-table-input">
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div id="product-submit-div">
                            <input type="reset" name="submitreset" id="product-submit" value="Clear">
                            <input type="submit" name="submit" id="product-submit" value="Add">
                        </div>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    </div>
</body>
</html>

This location of this page is root>admin>product_add.php. What seems to be the problem? Oh, I have also used tinymce text editor in the textarea.

vimuth
  • 5,064
  • 33
  • 79
  • 116
vilom
  • 51
  • 3
  • 9

1 Answers1

1

You are using Hyphen - signs in the HTML form for the field names; but when you try to read in PHP, you are using underscores.

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

Use below code instead of above

$product_cat = $_POST['product-cat'];
$product_brand = $_POST['product-brand'];
$product_title = $_POST['product-title'];
$product_price = $_POST['product-price'];
$product_desc = $_POST['product-description'];
$product_keywords = $_POST['product-keywords'];
Ekramul Hoque
  • 4,957
  • 3
  • 30
  • 31
Priyanka
  • 170
  • 1
  • 3
  • 12