1
votes

I have the following html code to upload a maximum of 8 images:

<div class="form-group">
<label class="col-sm-3 control-label">img 1</label>
    <div class="col-sm-6">
        <input type="file" name="img1" />
    </div>
 </div>
 <div class="form-group">
 <label class="col-sm-3 control-label">img 2</label>
     <div class="col-sm-6">
         <input type="file" name="img2" />
     </div>
 </div>
 ...... same for img3,4,5,6,8      

php code

if (array_key_exists('img1', $_FILES)) {
   if (in_array('image/jpg', $_FILES['img1']) && $_FILES['img1']['error'] == '0') {
      $img1 = "http://www.*.com/tmp/".$findid."_1.jpg";
      $name1 = $findid."_1.jpg";             
      move_uploaded_file($_FILES['img1']['tmp_name'], 'tmp/'.$name1);
   }
} 
...... same for img2,3,4,5,6,8   

<?php 
$images = explode(';', $ad['images']);
if ($images != NULL) {
   foreach ($images as $i => $filename) {
   echo '<a href="'.$filename.'" target="_blank" ><img src="'.$filename.'" width="60px" height="55px" style="float:left; border:1px solid #B1C4DF;" /></a>';
   }
}
?>

$images = implode(";",array_filter(array($img1,$img2,$img3,$img4,$img5,$img6,$img7,$img8)));
  if (isset($_GET['type']) && $_GET['update'] == 'true') {
      if($images != null){
        mysql_query("UPDATE ads SET title ='$title', images ='$images', description ='$description', priceValue ='$priceValue' WHERE id = '" . $_GET['id'] . "'") OR die(mysql_error());
        updateImages($un,$pw,$_GET['id'],$images);
      }else{
        mysql_query("UPDATE ads SET title ='$title', description ='$description', priceValue ='$priceValue' WHERE id = '" . $_GET['id'] . "'") OR die(mysql_error()); 
      }

      header("Location: edit.php?type=edit&id=" . $_GET['id'] . "");
  }

the problem is when I only upload img2 and save the form other existing images are removed from the database. When I upload img1 and img2 everything is fine. How can I edit the php code so it is possible to only upload img3 for example and retain img 1 and img2 ?

4
What did you try and what didn't work? Do you even understand the code you posted?Till Helge
etc for 2,3,4.... put that shotgun down!Flosculus

4 Answers

1
votes

fetch your data for images first and then update the details as follows

    $sql = "Select images from ads where id = '" . $_GET['id'] . "'";
    $q   = mysql_query($sql);
    if(mysql_num_rows($q) > 0){
        $res     = mysql_fetch_array($q);
        $imagesd = $res['images'];
        if(!empty($imagesd))
            $images  = $imagesd.';'.$images;
    }

    mysql_query("UPDATE ads SET title ='$title', images ='$images', description ='$description', priceValue ='$priceValue' WHERE id = '" . $_GET['id'] . "'") OR die(mysql_error());
0
votes

You could try changing the line

if (array_key_exists('img1', $_FILES)) {

with

if (array_key_exists('img1', $_POST)) {

and do so for all the inputs you have img2, img3... etc.

0
votes

Before uploading your images you need to validate and check which image your are uploading:

try below eg. instead of array_key_exists('img1', $_FILES)

eg:

if($_FILES["img1"]["tmp_name"]!='')
{
//do uploading //insert,update query
} 

else if($_FILES["img2"]["tmp_name"]!='')
{
//do uploading //insert,update query
} 
0
votes

You have a 8 inputs for upload image so make it array.it is easier and helpful to you.And when you upload images you write code 8 times to upload image, it is not good so change it like follow if you want.

First make array for all input

<input type="file" name="img[]" />

then in php

foreach($_FILES[ 'img' ][ 'name' ] as $key=>$img)
{
    if($img!="")
    {
         move_uploaded_file($_FILES['img']['tmp_name'][$key], 'tmp/'.$img);

         //and here update code
    }
}