dragon19720808
dragon19720808
2016-02-16 18:23

如何使用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 dongxi1680 5年前

    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>";
        }
    }
    
    点赞 评论 复制链接分享