2014-10-29 40 views
-1

簡單,因爲我在標題解釋我在每一個表格的最後有這樣的代碼具有相同的輸入名稱多種形式的輸出第一形式發佈信息

<input name="changety" class="chvf" type="hidden" value="1"><input name="itemid" value="<?= $result['id'] ?>" type="hidden"></form> 

,正如你看到的itemid正在導入從數據庫和有多種形式,使用此變量,但當我試圖訪問它屁股後_ $ _POST ['itemid']它總是會得到這第一個實例

我想知道是否有一種方式做這件事謝謝安德魯

整形貝爾ow:

<?php //things to do for below wrap all of these in a form instead of having a form for below 
     if($result['create_image_1'] || $result['create_image_2'] || $result['create_image_3'] || $result['create_image_4'] || $result['create_image_5'] || $result['create_image_6'] || $result['create_image_7'] || $result['create_image_8'] || $result['create_image_9'] || $result['create_image_10'] || $result['create_image_11'] || $result['create_image_12']){ 
      ?> 
       <form method="POST" enctype="multipart/form-data"><input onchange="this.form.submit()" name="update_image" id="upfiid" type="file" style="display:none;"/> 
      <?php 
     } 
    ?> 

<? if($result['create_image_1']){ ?><label onclick="chcaval(this, 1);" style="display:block;" for="upfiid">Phone cases</label><br /><? } ?> 
<? if($result['create_image_2']){ ?><label onclick="chcaval(this, 2);" style="display:block;" for="upfiid">T-Shirt</label><br /><? } ?> 
<? if($result['create_image_3']){ ?><label onclick="chcaval(this, 3);" style="display:block;" for="upfiid">Tote Bag</label><br /><? } ?> 
<? if($result['create_image_4']){ ?><label onclick="chcaval(this, 4);" style="display:block;" for="upfiid">Beach Bag</label><br /><? } ?> 
<? if($result['create_image_5']){ ?><label onclick="chcaval(this, 5);" style="display:block;" for="upfiid">Plate</label><br /><? } ?> 
<? if($result['create_image_6']){ ?><label onclick="chcaval(this, 6);" style="display:block;" for="upfiid">Mug</label><br /><? } ?> 
<? if($result['create_image_7']){ ?><label onclick="chcaval(this, 7);" style="display:block;" for="upfiid">Cup & Saucer</label><br /><? } ?> 
<? if($result['create_image_8']){ ?><label onclick="chcaval(this, 8);" style="display:block;" for="upfiid">Clock</label><br /><? } ?> 
<? if($result['create_image_9']){ ?><label onclick="chcaval(this, 9);" style="display:block;" for="upfiid">Cushions</label><br /><? } ?> 
<? if($result['create_image_10']){ ?><label onclick="chcaval(this, 10);" style="display:block;" for="upfiid">Framed Art Print</label><br /><? } ?> 
<? if($result['create_image_11']){ ?><label onclick="chcaval(this, 11);" style="display:block;" for="upfiid">Art Print</label><br /><? } ?> 
<? if($result['create_image_12']){ ?><label onclick="chcaval(this, 12);" style="display:block;" for="upfiid">Canvas</label><br /><? } ?> 

    <?php 
     if($result['create_image_1'] || $result['create_image_2'] || $result['create_image_3'] || $result['create_image_4'] || $result['create_image_5'] || $result['create_image_6'] || $result['create_image_7'] || $result['create_image_8'] || $result['create_image_9'] || $result['create_image_10'] || $result['create_image_11'] || $result['create_image_12']){ 
     ?> 
      <input name="changety" class="chvf" type="hidden" value="1"><input name="itemid[]" value="<?= $result['id'] ?>" type="hidden"></form> 
     <?php 
     } 
    ?> 






if($_FILES['update_image']){ 
    print_r($_POST['itemid']); 
    // Set some defaults here for the image size and type. 
    $allowedExtsImage = array('jpeg','jpg','gif','png'); 
    $allowedTypesImage = array('image/jpeg','image/pjpeg','image/png','image/gif'); 


    //$uploadPathImage = '/home/hygenie/public_html/uploads/'; 
    $uploadPathImage = $site_upload_domain.'/uploads/'; 


    $files = $_FILES['update_image']; 
    $FileName = $files['name']; 
    $FileType = strtolower($files['type']); 
    $FileTmpName = $files['tmp_name']; 
    $FileSize = $files['size']; 
    $FileExtension = strtolower(end(explode('.', $FileName))); 

    switch($_POST['changety']){ 

     case 1: 
     case 5: 
     case 6: 
      $maxUploadSizeImage = 2097152; //2mb 
      break; 

     case 8: 
      $maxUploadSizeImage = 3145728; //3mb 
      break; 

     case 2: 
     case 3: 
     case 4: 
     case 9: 
     case 10: 
     case 11: 
     case 12: 
      $maxUploadSizeImage = 4194304; //4mb 
      break; 

    } 
    if ($FileName) { 
     $fileName = time().'_'.rand(0,9999).'_'.str_replace(' ', '_', $FileName); 
     if (in_array($FileExtension, $allowedExtsImage) && in_array($FileType, $allowedTypesImage)) { 

      if ($FileSize > 0 && $FileSize <= $maxUploadSizeImage) { 
       if ($FileExtension == 'png') { 
        //replace white with transparent 
        $img = imagecreatefrompng($FileTmpName); 
        $whiteImg = imagecolorallocate($img , 255, 255, 255); 
        imagecolortransparent($img, $whiteImg); 
        $newImage = imagepng($img, $FileTmpName); 
        if (move_uploaded_file($FileTmpName,$uploadPathImage.$fileName)) { 
         $imagelocation1 = $fileName; 
        } 
       } 

       if (move_uploaded_file($FileTmpName,$uploadPathImage.$fileName)) { 
        $imagelocation1 = $fileName; 

       } 

       $getfilename = mysql_query("SELECT create_image_".$_POST['changety']." FROM items_z WHERE id=".$_POST['itemid'].""); 
       $oldfname = mysql_fetch_array($getfilename); 

       unlink($uploadPathImage.$oldfname['create_image_'.$_POST['changety']]); 
       $updateQuery = "UPDATE items_z SET create_image_".$_POST['changety']." = '".$fileName."' WHERE id=".$_POST['itemid'].""; 
       echo $updateQuery; 
       mysql_query($updateQuery); 


      } 
     } 
    } 


} 
+0

請發表更多代碼表格 – 2014-10-29 12:17:09

+0

是在'foreach()'語句中創建的表單嗎? – 2014-10-29 12:17:49

+0

在while語句中,我發佈了整個表格 – andrew196 2014-10-29 12:18:52

回答

0

它的原因是,隱藏變量只有一個'name'屬性,因此它越過了。

發佈的隱藏變量可能是您最後一次在循環中設置它。

你應該使用數組一樣:

<input name="changety[]" class="chvf" type="hidden" value="1"> 
<input name="itemid[]" value="<?= $result['id'] ?>" type="hidden"></form> 

而在後端PHP,做要緊。

+0

當我print_r這個變量它仍然只輸出這個變量的第一個實例像這樣Array([0] => 1940) – andrew196 2014-10-29 12:21:08

0

爲了解決這個問題,<input name="itemid[]" value="<?= $result['id'] ?>" type="hidden">在此輸入的名稱應該,的確是一個數組,但在你的情況,我可以看到的例子,它僅僅是在年底有一個[]平原名。服務器不解釋HTML代碼爲陣,因爲HTML沒有數組而且也不該服務器爲這一問題:)

解釋HTML您需要更改弄成這個樣子:

<input name="<?= itemid[$result['create_image_1']] ?>" value="<?= $result['id'] ?> 

,也,將if更改爲if() elseif() ...語句並更改每個$result的隱藏輸入的名稱,否則輸入的值不會更改。

另一種方式是創建多個表單,其中foreach()語句對提交按鈕具有不同的名稱。在這種情況下,您需要檢查if(isset($_POST['button_name'])),其中button_name對於每種形式都會有所不同。

希望這會有所幫助! :D

+0

它似乎不工作,當我做itemid [ ]作爲它在php中變成一個數組的名稱,但它只有一個值爲Array([0] => 1940) – andrew196 2014-10-29 12:36:30

+0

這是添加到文件中的代碼,它在該文件中被處理,如果這有助於 – andrew196 2014-10-29 12:37:51

+0

因爲您沒有調用它對於每個'$ result' ...它會在你每次調用它時覆蓋它,它將只顯示最後一個值...這個功能很容易理解......如果你不使用'foreach()',每次腳本進入該部分它將覆蓋值 – 2014-10-29 12:38:03

相關問題