简体   繁体   中英

form submitted data is not uploaded into database

I am using phpcode to get data from form and upload it into database. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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