2016-01-12 83 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'");