上传一个产品的五张图片

时间:2016-01-15 09:00:18

标签: php mysql

我想在我的产品详情页面上创建图像缩略图,如下所示:

enter image description here

问:如何修复此代码以上传一个产品的五张图片?

前:

===================================================
| id | name  |              img_file              |
---------------------------------------------------
| 1  | Shirt | shirt1.jpg, shirt2.jpg, shirt3.jpg |
===================================================

或者我应该创建一个用于存储图像文件名的新表吗?

moda_add.php

        <!-- Button to trigger modal -->
    <a class="btn btn-primary" href="#myModal" data-toggle="modal">Click Here To Add</a>
    <br>
    <br>
    <br>
    <!-- Modal -->
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">

    <h3 id="myModalLabel">Add</h3>
    </div>
    <div class="modal-body">

                    <form method="post" action="add.php"  enctype="multipart/form-data">
                    <table class="table1">
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">FirstName</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="p_name" placeholder="p_name" required /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">MiddleName</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="p_price" placeholder="p_price" required /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">LastName</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="p_discount" placeholder="p_discount" required /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Address</label></td>
                            <td width="30"></td>
                            <td><input type="text" name="p_descp" placeholder="p_descp" required /></td>
                        </tr>
                        <tr>
                            <td><label style="color:#3a87ad; font-size:18px;">Upload Image</label></td>
                            <td width="30"></td>
                            <td><input type="file" name="image" required /></td>
                        </tr>

                    </table>


    </div>
    <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button type="submit" name="Submit" class="btn btn-primary">Add</button>
    </div>


                    </form>
    </div>

add.php

<?php
ob_start();
session_start();
include('connect.php');
if($_SESSION['logged_in']) {
$ID=$_SESSION['member_id'];
}

                        if (!isset($_FILES['image']['tmp_name'])) {
                        echo "";
                        }else{
                        $file=$_FILES['image']['tmp_name'];
                        $image = $_FILES["image"] ["name"];
                        $image_name= addslashes($_FILES['image']['name']);
                        $size = $_FILES["image"] ["size"];
                        $error = $_FILES["image"] ["error"];

                        if ($error > 0){
                                    die("Error uploading file! Code $error.");
                                }else{
                                    if($size > 10000000) //conditions for the file
                                    {
                                    die("Format is not allowed or file size is too big!");
                                    }
                                else
                                    {
                                move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);          
                                $p_img=$_FILES["image"]["name"];
                                $p_name= $_POST['p_name'];
                                $p_price= $_POST['p_price'];
                                $p_discount= $_POST['p_discount'];
                                $p_descp= $_POST['p_descp'];
                                //$email= $_POST['email'];

                    mysql_query("insert into fm_product (p_name,p_price,p_discount,p_descp,p_img,p_member_id,p_created_date) 
                    values('$p_name','$p_price','$p_discount','$p_descp','$p_img', '$ID', CURRENT_TIMESTAMP)")or die(mysql_error());

                                }
                                    header('location:product.php');
                                }
                        }
?>                              

2 个答案:

答案 0 :(得分:2)

切勿在一列中添加多个数据。这样做如下:

enter image description here

然后,您可以使用以下查询轻松抓取图像:

SELECT img_name FROM fm_product_image WHERE product_id = [ID]

答案 1 :(得分:1)

创建一个包含Snoproduct_Idproduct_image coloumns的表'fm_product_image'。在此处,在“product_Id”列中,插入产品的productId

按照我的步骤。

moda_add.php (添加name="image[]"

<td><input type="file" name="image[]" multiple required /></td>

<强> add.php

<?php
ob_start();
session_start();
include('connect.php');
if($_SESSION['logged_in']) {
    $ID=$_SESSION['member_id'];
}

if (!isset($_FILES['image']['tmp_name'])) {
    echo "";
}
else
{
    $p_name= $_POST['p_name'];
    $p_price= $_POST['p_price'];
    $p_discount= $_POST['p_discount'];
    $p_descp= $_POST['p_descp'];    

    mysql_query("insert into fm_product (p_name,p_price,p_discount,p_descp,p_member_id,p_created_date) 
                values('$p_name','$p_price','$p_discount','$p_descp', '$ID', CURRENT_TIMESTAMP)") or die(mysql_error());    

    $ProductId = mysql_insert_id();

    $TotalImage=count($_FILES['image']['name']);

    for($i=0;$i<$TotalImage;$i++)
    {
        $image_name = $_FILES['image']['name'][$i]; 
        $image_to_db = 'upload/'.$image_name;   
        if(move_uploaded_file($_FILES['image']['tmp_name'][$i],'upload/'.$image_name))
        {
            mysql_query("INSERT INTO fm_product_image SET product_Id='$ProductId', product_image='$image_to_db'");
        }
    }
    header('location:product.php');
}
?>