2016-01-15 33 views
-2

我想我的產品詳細信息頁面上創建圖像縮略圖像這樣:上傳五張圖片爲一個產品

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'); 
           } 
         } 
?>        
+1

那麼,是什麼問題? – Epodax

+1

「或者我應該創建一個用於存儲圖像文件名的新表?」 - 是的,創建新的表'product_images',其中將包含每個產品的所有圖像名稱 – RomanPerekhrest

+3

_或者我應該創建一個新的表來存儲圖像文件名?_:當然。一個表格單元格不應包含用逗號分隔的多個值。 –

回答

1

創建一個表'fm_product_image'Snoproduct_Idproduct_image coloumns。在這裏,在'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'); 
} 
?> 
+0

非常感謝你,我會試試這個。 –

+0

試試這個。我相信你不會有任何問題@ButterToast。迴應我。 *所有的最佳* –

+0

它不起作用:( –

2

決不在一列中添加多個數據片。做到像以下:

enter image description here

然後你就可以很容易地通過下面的查詢抓取圖片:

SELECT img_name FROM fm_product_image WHERE product_id = [ID]

+0

如何在fm_product之間插入多個表和fm_product_image? –

+0

您只需在行中插入行,其中'fm_product_image'中的'product_id'是'fm_product'中的'id' – Matt

+0

您是否有上傳代碼示例?我使用foreach但出錯。 :( –