2016-03-08 54 views
1

我想上傳一個圖片,並在名爲圖像的文件夾內創建它的副本,但它不會在循環過程中工作,你能幫我解決問題嗎?這是我的代碼;文件類型輸入不工作在一個循環

$sql="SELECT * FROM product"; 
$q=$conn->query($sql); 
while($r=$q->fetch(PDO::FETCH_ASSOC)) 
{ 
$code= $r['prod_code']; 
    <table class="table" width=200% > 
            <tr> 
            <th colspan=6 style="background-color: lightgray"> </th> 
            </tr> 
            <tr> 
            <th>Add variant </th> 
            </tr> 
            <tr> 
            <th>Image</th> 
            <th>color</th> 
            <th>Size</th> 
            <th>Size Type</th> 
            <th>Quantity</th> 
            <th>Action</th> 
            </tr> 
            <tr> 
            <td><input type="file" name="image" id="image" style="width: 80px" required /></td> 
            <form method="POST"> 
            <td><input type="text" name="addcolor" class="form-control" style="width: 150px; border-radius: 0px" required/></td> 
            <td><input type="text" name="addsize" class="form-control" style="width: 150px; border-radius: 0px " required/></td> 
            <td><select name="addtype" class="form-control" style="width: 150px; border-radius: 0px" required ><option value="International">International </option> 
            <option value="US">US </option> 
            <option value="UK">UK</option> 
            </select></td> 
            <td><input type="text" name="addquantity" class="form-control" style="width: 150px; border-radius: 0px" required/></td> 
            <td><button type="submit" class="btn btn-success" name="addprod" value="<?php echo $code; ?>" ><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button></td> 
            </form> 
            </tr> 

            </table> 
} 

繼承人的PHP:

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

      $prodcode= $_POST['addprod']; 
      $color= $_POST['addcolor']; 
      $size= $_POST['addsize']; 
      $type= $_POST['addtype']; 
      $quantity= $_POST['addquantity']; 

      $image=addslashes(file_get_contents($_FILES['image']['tmp_name'])); 
      $image_name=addslashes($_FILES['image']['name']); 
      $image_size=getimagesize($_FILES['image']['tmp_name']); 
      move_uploaded_file($_FILES['image']['tmp_name'],"images/".$_FILES['image']['name']); 
      $location="images/".$_FILES['image']['name']; 

      $sql7 = "INSERT into color_variation (prod_code,color_pic,color,size,size_type,quantity) values('$prodcode','$location','$color','$size','$type','$quantity')"; 
      $q7 = $conn->query($sql7); 
     } 

只會保存folderand的斜線的名稱,它產生的錯誤。請幫我這個請

+0

人?請hu hu' –

+0

你能檢查$ _FILES的內容嗎? –

+1

你有PHP和HTML混合。 PHP代碼應該包含在'<?php'和'?>'中。另請注意,只有一個具有相同名稱的輸入將被處理。你可能會想給這些獨特的名字。 – Jim

回答

1

你有錯誤到你的第一段代碼,我解決它,它看起來像這樣。我公司還爲輸入更名(從addcoloraddcolor[])與他們同列到你的第二條代碼工作(見如何work with multi-dimensional array post here):

<?php 

$sql = "SELECT * FROM product"; 
$q = $conn->query($sql); 
while ($r = $q->fetch(PDO::FETCH_ASSOC)) { 
    $code = $r['prod_code']; ?> 
    <table class="table" width=200%> 
     <tr> 
      <th colspan=6 style="background-color: lightgray"></th> 
     </tr> 
     <tr> 
      <th>Add variant</th> 
     </tr> 
     <tr> 
      <th>Image</th> 
      <th>color</th> 
      <th>Size</th> 
      <th>Size Type</th> 
      <th>Quantity</th> 
      <th>Action</th> 
     </tr> 
     <tr> 
      <td><input type="file" name="image[]" id="image" style="width: 80px" required/></td> 
      <form method="POST"> 
       <td><input type="text" name="addcolor[]" class="form-control" style="width: 150px; border-radius: 0px" 
          required/></td> 
       <td><input type="text" name="addsize[]" class="form-control" style="width: 150px; border-radius: 0px " 
          required/></td> 
       <td><select name="addtype[]" class="form-control" style="width: 150px; border-radius: 0px" required> 
         <option value="International">International</option> 
         <option value="US">US</option> 
         <option value="UK">UK</option> 
        </select></td> 
       <td><input type="text" name="addquantity[]" class="form-control" style="width: 150px; border-radius: 0px" 
          required/></td> 
       <td> 
        <button type="submit" class="btn btn-success" name="addprod[]" value="<?php echo $code; ?>"><span 
          class="glyphicon glyphicon-ok" aria-hidden="true"></span></button> 
       </td> 
      </form> 
     </tr> 

    </table> 
<?php } ?> 
+0

與現在一樣如何解決我的問題? –

+0

您還需要像使用多個數組一樣處理您的發佈數據(請參閱此問題的答案:http://stackoverflow.com/questions/1719087/multi-dimensional-array-post-from-form) –

+0

比這麼多,那是我忘了。 –