2016-01-12 84 views
-1

我有5個輸入字段,上傳圖片後,我將採取所有值並將名稱存儲在一列(使用implode)中。當我想編輯表單時,如果我只編輯一個輸入,使其他列在列中相同。怎麼做??有兩個條件的輸入文件

for($i=0;$i<5;$i++){ 
$tmp_name = $_FILES["photo"]["tmp_name"][$i]; 
if($tmp_name != null){ 
    $img = 'g'.$random.$_FILES["photo"]["name"][$i]; // image name thats going to save in db and folder. 
move_uploaded_file($tmp_name,$folder.$img); 
$images_name =$images_name."~~".$img; 
    $imageup=mysql_query("update pg_details set gallery='$images_name' where id='$id'"); 
} 
} 

上面的代碼適用於輸入上傳和當我想更新我想其他名稱保持不變。

回答

2

如果你只是更新圖像,那麼它會這樣做。此外,您正在發送SQL查詢以在每個循環更新數據庫。循環完成時執行此操作。

//fetch the existing images first 
$imagedown = mysql_fetch_assoc(mysql_query("SELECT gallery FROM pg_details WHERE `id`='".$id."'")); 
$images = explode("~~", $imagedown['gallery']); 

for($i=0;$i<5;$i++){ 
    $tmp_name = $_FILES["photo"]["tmp_name"][$i]; 
    if($tmp_name != null){ 
     $img = 'g'.$random.$_FILES["photo"]["name"][$i]; // image name thats going to save in db and folder. 
    move_uploaded_file($tmp_name,$folder.$img); 
    //replace the respective index with the image it's to be replaced with 

    if(isset($images[$i])){ 
     $images[$i] = $img; 
    }else{ 
     array_push($images, $img); 
    } 
} 
    $images_name = implode("~~", $images); 
    //$images_name =$images_name."~~".$img; 
    $imageup=mysql_query("update pg_details set gallery='$images_name' where id='$id'");