0

Code:

<!DOCTYPE html >
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
 <script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
  <script>tinymce.init({ selector:'textarea' });</script>
</head>

<body>

<form action="insert_product.php" method="post" enctype="multipart/form-data">
    <table>
            <tr>
                <td>Name</td>
                <td><input type="text" name="product_title" required="required"></td>

            </tr>

            <tr>
                <td>category</td>
                <td><select name="product_category" >
                <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_data']; 


                        echo "

                        <option>$cat_title </option>

                        ";
                        }
                ?>


                </select></td>

            </tr>


            <tr>
                <td>featured</td>
                <td><select name="product_featured" >
                <option>0</option>
                <option>1</option>
                </select></td>

            </tr>

            <tr>
                <td>price</td>
                <td><input type="text" name="product_price" required="required"></td>

            </tr>

            <tr>
                <td>image</td>
                <td><input type="file" name="product_image" required="required"></td>

            </tr>

            <tr>
                <td>keywords</td>
                <td><input type="text" name="product_keywords" required="required"></td>

            </tr>


            <tr>
                <td>description</td>
                <td><textarea name="product_description" cols="20" rows="10" ></textarea></td>

            </tr>

            <tr>
                <td><input type="submit" value="Add Product" name="insert_post"></td>
            </tr>

    </table>
</form>

</body>

</html>

<?php 
        if( isset($_POST['insert_post']))
        {

            $product_title = $_POST['$product_title'];
            $product_category = $_POST['$product_category'];
            $product_featured = $_POST['product_featured'];
            $product_price = $_POST['$product_price'];
            $product_keywords = $_POST['$product_keywords'];
            $product_description = $_POST['$product_description'];
            $product_image = $_FILES['product_image']['name'];
            $product_image_tmp = $_FILES['product_image']['tmp_name'];


        echo  $insert_product = " insert into products (product_cat, product_featured, product_title, product_price, product_desc,product_image,product_keywords) values ('$product_category','$product_featured','$product_title','$product_price','$product_description','$product_image','$product_keywords','$product_keywords')";
        }
 ?>

this is the result I get from this code when I print it with echo

insert into products (product_cat, product_featured, product_title, product_price, product_desc,product_image,product_keywords) values ('','1','','','','champagne culture logo men.png','','')

can someone please tell me what I'm doing wrong? I've checked repeatedly checked to see if i had any spelling errors or anything of that nature but I cant seem to identify exactly what it is that I am doing wrong. Any help you could provide would be greatly appreciated

coder
  • 8,346
  • 16
  • 39
  • 53
Oshane Baker
  • 35
  • 1
  • 1
  • 8
  • Don't "guess" what you're doing wrong. Get [errors to display](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and look at what PHP tells you. If you had, you would have noticed undefined index errors. – Mike May 10 '18 at 00:58
  • remove `$` inside your `$_POST['$product_title']` – hungrykoala May 10 '18 at 01:54

2 Answers2

0

Remove the $ part from the key:

        $product_title = $_POST['$product_title'];
        $product_category = $_POST['$product_category'];
        $product_featured = $_POST['product_featured'];
        $product_price = $_POST['$product_price'];
        $product_keywords = $_POST['$product_keywords'];
        $product_description = $_POST['$product_description'];

Should be

        $product_title = $_POST['product_title'];
        $product_category = $_POST['product_category'];
        $product_featured = $_POST['product_featured'];
        $product_price = $_POST['product_price'];
        $product_keywords = $_POST['product_keywords'];
        $product_description = $_POST['product_description'];
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
0

You need to change this

$product_title = $_POST['$product_title'];
$product_category = $_POST['$product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['$product_price'];
$product_keywords = $_POST['$product_keywords'];
$product_description = $_POST['$product_description'];
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];

to this

$product_title = $_POST['product_title'];
$product_category = $_POST['product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['product_price'];
$product_keywords = $_POST['product_keywords'];
$product_description = $_POST['product_description'];
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];

NOTE

if there was no error, you need to make sure if the input is not empty, you can use empty() property to check if the variable/input is empty.

like

if(empty($_POST['product_title'])) {
    echo "product title shouldn't empty";
}