使用php接收空表单值并发布

时间:2016-10-13 07:02:40

标签: php forms post

我正在制作一个插入表单,以便将产品添加到mysql数据库中。我使用echo命令打印查询。打印的查询显示如下的空值:

  

注意:未定义的索引:第40行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php中的product_cat

     

注意:未定义的索引:product_brand in   第41行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php

     

注意:未定义的索引:product_title in   第42行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php

     

注意:未定义的索引:product_price in   第43行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php

     

注意:未定义的索引:product_description in   第44行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php

     

注意:未定义的索引:product_keywords in   第45行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php

     

注意:未定义的索引:product_images in   第48行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php

     

注意:未定义的索引:product_images in   第49行的F:\ Apache24 \ htdocs \ Site1 \ admin \ product_add.php

     

插入产品('product_cat','product_brand','product_title',   'product_price','product_desc','product_image','product_keywords')   值('','','','','','','')

这是html表单。它也是行动页面:

<!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>

此页面的此位置为root&gt; admin&gt; product_add.php。什么似乎是问题? 哦,我还在textarea中使用了tinymce文本编辑器。

1 个答案:

答案 0 :(得分:1)

您在HTML表单中使用连字符-符号作为字段名称;但是当你尝试用PHP读取时,你正在使用下划线。

$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_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'];