duanmu5039 2016-11-06 17:37
浏览 75

如何使用HTML表单添加图像文件并将文件放入数据库

<!DOCTYPE>
<?php
    include("includes/dbconfig.php")
?>
<html>
<head>
    <title>Inserting Product</title>
</head>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
<body bgcolor="skyblue">    
<form action = "insert_product.php" method="post" enctype="multipart/form-data">    
    <table align="center" width="700" border="2" bgcolor="orange" >
        <tr align = "center">
            <td colspan="8"><h2>Insert New Product 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" required>
                    <option>Select a Category</option>
                <?php
                    $get_cats = "SELECT * FROM categories";
                    $run_cats = mysqli_query($con, $get_cats);

                    while ($row_cats = mysqli_fetch_array($run_cats)){
                        $cat_id = $row_cats['cat_id'];
                        $cat_title = $row_cats['cat_title'];
                        echo"<option value ='$cat_title'>$cat_title</option>";
                    }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right"><b>Product Brand:</b></td>
            <td>
                <select name="product_brand" required>
                    <option>Select a Brand</option>
                <?php
                    $get_brands = "SELECT * FROM brands";
                    $run_brands = mysqli_query($con, $get_brands);

                    while ($row_brands = mysqli_fetch_array($run_brands)){
                        $brand_id = $row_brands['brand_id'];
                        $brand_title = $row_brands['brand_title'];
                        echo"<option value='$brand_title'>$brand_title</option>";
                    }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right"><b>Product Image:</b></td>
            <td><input type = "file" name="product_img" required/></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="8"><input type = "submit" name="insert_post" value="Add Product" /></td>
        </tr>
    </table>
</form>
</body>
</html>

<?php    
if(isset($_POST['insert_post'])){
    //get text data from 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'];

    $insert_product = "INSERT into products(product_cat, product_brand, product_title, product_price, product_img, product_desc, product_keywords) VALUES ('$product_cat', '$product_brand', '$product_title', '$product_price', '$product_img', '$product_desc', '$product_keywords')";

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

    if($insert_pro){
        echo "<script>alert('Product has been inserted')</script>";
        echo "<script>window.open('insert_product.php','_self')</script>";
    }       
}
?>

I am trying to add an image. As you can see on it is supposed to display a alert message stating "product has been inserted" when I removed everything that had to do with product_img it uploaded properly when i include product_img it just empties out all the fields and that's it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图