简体   繁体   English

表单提交的数据未上载到数据库

[英]form submitted data is not uploaded into database

I am using phpcode to get data from form and upload it into database. 我正在使用phpcode从表单获取数据并将其上传到数据库。 But the data is not saved in database.Here 'insert_product.php' is the file where the entire code resides and form is redirected to same page after button is clicked. 但是数据没有保存在数据库中。这里的“ insert_product.php”是整个代码所在的文件,单击按钮后将表单重定向到同一页面。 Anyone can help? 有人可以帮忙吗?

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

         }
    }








?>

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

You need to connect to database first like so: 您需要先连接到数据库,如下所示:

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

More on database connections here . 有关数据库连接的更多信息,请参见 And if I was you I would start simple by first getting the connection working, then inserting data into to the table and only when you have a good grasp of those I would move onto uploading files. 如果我是您,那么我将首先通过使连接开始工作,然后将数据插入表中开始,并且只有在您掌握了这些知识之后,我才会着手进行上载文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM