簡體   English   中英

表單提交的數據未上載到數據庫

[英]form submitted data is not uploaded into database

我正在使用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>";

         }
    }








?>

如果這是完整的代碼,我找不到類似$con = mysqli_connect(...)顯然,您需要在執行任何查詢之前連接到數據庫

您需要先連接到數據庫,如下所示:

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

有關數據庫連接的更多信息,請參見 如果我是您,那么我將首先通過使連接開始工作,然后將數據插入表中開始,並且只有在您掌握了這些知識之后,我才會着手進行上載文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM