0

我有 12 张图片,我想用表格更新/编辑它们。我选择更新一个或多个但不是全部,但将旧图像名称保留在数据库中。

我尝试了以下代码,但是当我更新一两个图像时,它不会保留旧图像并且表字段为空。

foreach($_FILES['more_images']['name'] as $key => $post_image) {
        $getOld_ImageName = mysql_query("SELECT `image` FROM `more_images` WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
        $getOld_Image = mysql_fetch_array($getOld_ImageName);
        $more_imageOld = $getOld_Image['image'];

        if (isset($post_image)) {
            $file_name = $_FILES['more_images']['name'][$key];
            $file_tmp  = $_FILES['more_images']['tmp_name'][$key];

            if($file_name == $more_imageOld) {
                $updateMore_Images = mysql_query("UPDATE `more_images` SET `image` = '$more_imageOld' WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
            } else {
                move_uploaded_file($file_tmp, "$uploaddir/$file_name");
                $updateMore_Images = mysql_query("UPDATE `more_images` SET `image` = '$file_name' WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
            }
        } else {
            if ($key == $getOld_Image['key']) {
                $post_image = $more_imageOld;
                $updateMore_Images = mysql_query("UPDATE `more_images` SET `image` = '$post_image' WHERE `car_id` = '$id' AND `key` = '$key'") or die(mysql_error());
            }
        }
    }

返回新的图像名称,并在我保留的$_FILE图像上留空。

0 = 
1 = 4061857_1_b.jpg
2 = 4061857_2_b.jpg
3 = 4061857_3_b.jpg
4 = 4061857_4_b.jpg
5 = 4061857_5_b.jpg
6 = 
7 = 
8 = 
9 = 
10 = 
11 = 

换句话说,我试图更新表格并且每个$key都是空白的,以保持表格中的前一个图像名称不变。

有什么建议么 ?

4

1 回答 1

0

我改变了if (isset($post_image))if (!empty($post_image))并且它起作用了。

于 2013-11-08T00:30:53.440 回答