-2

I want to update multiple image in php in database store this image as string format 2.png,3.png,8.png like this now i want to update that image issue am facing is when try to update with that image when i put empty image it took also empty image what should i have to do this is my code

    for ($i = 0; $i < count($_FILES['files']['name']); $i++)
    {
    // echo "hello";
    // echo $i;

    $finalcnt=$i;

    $temp = $_FILES["files"]["tmp_name"][$i];


    $name = $_FILES["files"]["name"][$i];

    // if(empty($name))
    $stmt = $con->query("SELECT imgName FROM multiple where id=18 ");

    $row = $stmt->fetch_assoc();
    $getname=$row['imgName'];
    $covrt=explode(",",$getname);


    $hello = $_FILES["files"]["name"];
    $jjjjjgg=implode(",",$hello);
    if($i == 0){ $err = "File uploaded successfully"; $cls = "success"; }
    move_uploaded_file($temp,"uploads/".$name);


    $after_implode= implode(",",$hello);

    }

    print_r($covrt);//old array list
    print_r($hello);//New  array list


    for ($i = 0; $i < count($_FILES['files']['name']); $i++)
    {
    // echo "hello";
    // echo $i;

    $finalcnt=$i;

    $temp = $_FILES["files"]["tmp_name"][$i];


    $name = $_FILES["files"]["name"][$i];

    // if(empty($name))
    $stmt = $con->query("SELECT imgName FROM multiple where id=18 ");

    $row = $stmt->fetch_assoc();
    $getname=$row['imgName'];
    $covrt=explode(",",$getname);


    $hello = $_FILES["files"]["name"];
    $jjjjjgg=implode(",",$hello);
    if($i == 0){ $err = "File uploaded successfully"; $cls = "success"; }
    move_uploaded_file($temp,"uploads/".$name);


    $after_implode= implode(",",$hello);

    }

Thank You In Advanced

I tried with may time

  • 3
    Could you please use a bit of punctuation in what you write, such endless snake sentences are hard to read. – CBroe May 22 '23 at 13:09
  • Agreed. I've read it 3 times and I'm still a bit unsure what you're trying to describe. See also [ask] – ADyson May 22 '23 at 13:13
  • _"when try to update with that image when i put empty image it took also empty image"_ - you mean, when you don't upload a new image for position X - then you don't want an empty string between the commas in your result in that position, but you want to keep the already existing image? It currently looks like you read the string from the database each time, can't make much sense of this; if anything, you should read it once, explode into an array, then change elements in there for positions where you _do_ have a new image, and then implode and write the whole thing back ... – CBroe May 22 '23 at 13:13
  • But before you proceed with this, you should really think your data model over. [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/q/3653462/1427878) - short answer: Yes. Do yourself the favor of fixing this _now_, at any later point it will take even more time and effort. – CBroe May 22 '23 at 13:14

1 Answers1

0
    // First array
    $array1 = ['John', '', 'Lisa', '', 'Mike'];

    // Second array
    $array2 = ['Doe', 'Smith', 'Williams'];

    // Iterate through array1
    for ($i = 0; $i < count($array1); $i++) {
      // Check if the value at the current index is empty
      if (empty($array1[$i])) {
        // Assign the corresponding value from array2 to array1
        if (isset($array2[$i])) {
          $array1[$i] = $array2[$i];
        }
      }
    }

    // Output the updated array1
    print_r($array1);