0

I need to delete image name from database which is in comma separated form

I used below approach..

names is in database : 31.jpg,31 copy.jpg,110 copy.jpg,110.jpg,110 copy-1.jpg,

After explode it :

Array
(
    [0] => 31.jpg
    [1] => 31 copy.jpg
    [2] => 110 copy.jpg
    [3] => 110.jpg
    [4] => 110 copy-1.jpg
    [5] => 
)

To delete img name with comma : 110.jpg,

Now result : 31 copy.jpg,110 copy.jpg,110.jpg,110 copy-1.jpg,

But why is 110.jpg in result?

Code for above approach.

<?php

    require("include/config.inc.php");

    $parent_img_id = 184;
    $child_imgs_id = 1221;
    $delimg = '110.jpg';

    $getInfoChild_imgs = mysql_query("select * from parent where parent_img_id = '$parent_img_id' and child_imgs_id = '$child_imgs_id' and child_imgs like '%$delimg%'") or die(mysql_error());

        if(mysql_num_rows($getInfoChild_imgs)>0){
            $result = mysql_fetch_array($getInfoChild_imgs);

            $child_imgs_name =$result['child_imgs'];
            $child_imgs_name = explode(',', $child_imgs_name); // Explode into array


            $deleteImgName = $delimg.',';

            unset($child_imgs_name[ array_search($deleteImgName, $child_imgs_name) ]);

            $child_imgs_name = implode(',',$child_imgs_name); 

     }
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Naresh
  • 2,761
  • 10
  • 45
  • 78

3 Answers3

4

$deleteImgName is 110.jpg,

The array doesn't have 110.jpg,. It has 110.jpg

When you explode using ,, the elements don't contain any commas.

Change $deleteImgName = $delimg.',' to $deleteImgName = $delimg;,

Osiris
  • 4,195
  • 2
  • 22
  • 52
  • Also, be warned that if the name doesn't exist for X reason, you should probably validate it first. if (in_array($deleteImgName, $child_imgs_name)) { unset($sheetData[array_search($deleteImgName], $child_imgs_name)]); } – Manatax Dec 06 '12 at 08:46
1

remove the comma from the delete image variable. I haven't checked but since you exploded the commas they will be gone from the array

$deleteImgName = $delimg; #.',';
0

You are searching for $deleteImgName where you appended a "," that is not part of the array entry after exploding the string.

dognose
  • 20,360
  • 9
  • 61
  • 107