表单提交的数据不会上传到数据库中

时间:2016-06-07 09:01:40

标签: php

我使用phpcode从表单中获取数据并将其上传到数据库中。但是数据没有保存在数据库中。这里是' insert_product.php'是单击按钮后整个代码所在的文件,并将表单重定向到同一页面。有人可以帮忙吗?

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

        <table align="center" width="795" border="2" bgcolor="#187eae">

            <tr align="center">
                <td colspan="7"><h2>Insert New Post Here</h2></td>
            </tr>

            <tr>
                <td align="right"><b>Product Title:</b></td>
                <td><input type="text" name="product_title" size="60" required/></td>
            </tr>

            <tr>
                <td align="right"><b>Product Category:</b></td>
                <td>
                <select name="product_cat" >
                    <option>Select a Category</option>
                    <option value="1">Laptop</option>
                    <option value="2">Computer</option>
                </select>


                </td>
            </tr>

            <tr>
                <td align="right"><b>Product Brand:</b></td>
                <td>
                <select name="product_brand" >
                    <option>Select a Brand</option>
                    <option value="1">LG</option>
                    <option value="2">Samsung</option>
                </select>


                </td>
            </tr>

            <tr>
                <td align="right"><b>Product Image:</b></td>
                <td><input type="file" name="product_image" /></td>
            </tr>

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

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

            <tr>
                <td align="right"><b>Product Keywords:</b></td>
                <td><input type="text" name="product_keywords" size="50" required/></td>
            </tr>

            <tr align="center">
                <td colspan="7"><input type="submit" name="insert_post" value="Insert Product Now"/></td>
            </tr>

        </table>


    </form>


</body>
</html>
<?php

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

        //getting the text data from the fields
        $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_keywords = $_POST['product_keywords'];

        //getting the image from the field
        $product_image = $_FILES['product_image']['name'];
        $product_image_tmp = $_FILES['product_image']['tmp_name'];

        move_uploaded_file($product_image_tmp,"product_images/$product_image");

         $insert_product = "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_image','$product_keywords')";

         $insert_pro = mysqli_query($con, $insert_product);

         if($insert_pro){

         echo "<script>alert('Product Has been inserted!')</script>";
         echo "<script>window.open('index.php?insert_product','_self')</script>";

         }
    }








?>

2 个答案:

答案 0 :(得分:1)

如果这是整个代码,我找不到像$con = mysqli_connect(...)这样的内容 显然,您需要在对数据库执行任何查询之前连接到数据库

答案 1 :(得分:0)

您需要首先连接到数据库:

$con = mysqli_connect("localhost","my_user","my_password","my_db");

有关数据库连接的更多信息here。如果我是你,我会先简单地开始连接工作,然后将数据插入到表中,并且只有当你很好地掌握了上传文件时才会这样做。