dragon19720808 2016-02-16 18:23
浏览 87
已采纳

如何使用PHP将多个图像上传到mysql数据库

i am making an ecommerce site, i want to upload multiple images that can be used to show the products. my problem is that it only uploads one image where it is stored to the file i directed 'product_images' to and its details are stored in the db. only one. i want to be able to upload atleast 4. how can i do so?

Code: FORM:

                  <form method="post" action="" enctype="multipart/form-data">
                      <div class="col-lg-6">    
                          <p>PRODUCT DETAILS:</p> <br />

                      <tr>
        <div class="form-group">
                <label for="position">Enter Product Name:</label>
                <div class="input-group">
                    <input type="text" class="form-control" name="prod_name" id="prod_name" placeholder="Enter Product Name:" required>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
                </div>
            </div>

    </tr>
    <tr>
        <div class="form-group">
                <label for="position">Enter Product Brand:</label>
                <td><select type="position" class="form-control" id="prod_brand" name="prod_brand">
                        <OPTION value="Select Model..."></OPTION>
                        <?php brand(); ?>
                    </select></td>

            </div>

    </tr>
    <tr>
        <div class="form-group">
                <label for="position">Enter Product Model:</label>
                <td><select type="position" class="form-control" id="prod_model" name="prod_model">
                        <OPTION value="Select Model..."></OPTION>
                        <?php query(); ?>
                    </select></td>
                    <?php close(); ?>
            </div>

    </tr>
             <tr>
        <div class="form-group">
                <label for="position">Enter Product Price:</label>
                <div class="input-group">
                    <input type="text" class="form-control" name="prod_price" id="prod_price" placeholder="Enter Product Price:" required>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
                </div>
            </div>

    </tr>
    <tr>
        <div class="form-group">
                <label for="position">Enter Product Description:</label>
                <div class="input-group">
                    <textarea style="width: 500px; height: 251px;" type="text" class="form-control" name="prod_desc" id="prod_desc" placeholder="Enter Product Description:" cols="20" rows="5" ></textarea>
                </div>
            </div>

    </tr>

     <tr>
        <div class="form-group">
                <label for="position">Enter Product Quantity:</label>
                <div class="input-group">
                    <input type="text" class="form-control" name="prod_qty" id="prod_qty" placeholder="Enter Product Quantity:" required>
                    <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
                </div>
            </div>
            <div class="form-group"><tr>
                    <td><label for="position">Select Images:</label></td>
                    <td><input type="file" name="product_image[]" id="product_image" multiple=""></td>                        
            </div>
                  <!--<div class="form-group">
                        <input type="file" id="file-1" class="file" multiple name="product_image[]">
                    </div>-->
            </tr>
    </tr>
                        <tr>
        <br /><td><input class="btn btn-info" type='submit' name='submit' value='Add Product' /></td>
    </tr>
                  </form>

INSERTING THE PRODUCT:

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

    $prod_name = strtoupper($_POST['prod_name']);
    $prod_brand = $_POST['prod_brand'];
    $prod_cat = $_POST['prod_model'];
    $prod_price = $_POST['prod_price'];
    $prod_desc = $_POST['prod_desc'];
    $prod_qty = $_POST['prod_qty'];
    $d = date("Y-m-d");

    $id_query = $connection->query("INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, date) 
VALUES ('$prod_cat','$prod_brand','$prod_name','$prod_price','$prod_desc','$prod_qty','$d')");

    $query = mysql_insert_id();     


    $rand = rand(5,9878967699);
foreach($_FILES['product_image']['tmp_name'] as $key => $tmp_name){

    $name = $_FILES['product_image']['name'][$key];
    $tmpname = $_FILES['product_image']['tmp_name'][$key];
    $type = $_FILES['product_image']['type'][$key];
    $size = $_FILES['product_image']['size'][$key];


    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $rand_dir_name = substr(str_shuffle($chars), 0, 15); //will pick the random 15 first characters
    mkdir("product_images/$rand_dir_name/");

    $dir = "product_images/$rand_dir_name/";

    $move = move_uploaded_file($tmpname, $dir.$name);

    if($move) {
        //$query = mysql_query("insert into products (product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date) 
//VALUES('$prod_cat','$prod_brand','$prod_name','$prod_price','$prod_desc','$prod_qty','$rand_dir_name/$name',now())");
    $query = $connection->query("update products set product_image='$rand_dir_name/$name' where product_title='$prod_name'");

if($query) {
        echo "<script>alert('Adding a product is successful!!!.')</script>";
    } else {
        echo "<script>alert('Adding a product is unsuccessful!!!.')</script>"; 
    }
    } else {
        echo 'Picture upload failed';
    }
    }
    }
  • 写回答

1条回答 默认 最新

  • dongxi1680 2016-02-16 19:50
    关注

    You could make another table to store the image links in and 'connect' it to the table products by its product id.

    So:

    table 'Products'
    ID, ProductName, optional other columns for product details
    
    table 'Images'
    ID, ProductID, ImageSrc
    

    Then select all products. And inside the while loop you select per product all corresponding images by ProductID.

    This way you can store unlimited images per product.

    If you only want 4 images, then just make 4 columns for the image links

    Update 1: answer on comment

    At first, start using PDO or otherwise mysqli instead of mysql.

    Select your images like this

    $products = mysqli_query($conn, "SELECT * FROM products");
    if (mysqli_num_rows($products) > 0) {
        while ($product = mysqli_fetch_assoc($products)) {
            echo "<p>Images for ".$product['ProductName'].":<br>";
            $images = mysqli_query($conn, "SELECT * FROM images WHERE ProductID = ".$product['ID']."");
            if (mysqli_num_rows($images) > 0) {
                while ($image = mysqli_fetch_assoc($images)) {
                    echo "- ".$image['ImageSrc']."<br>";
                }
            } else {
                echo "No images";
            }
            echo "</p>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算