我有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
都是空白的,以保持表格中的上一个图像名称不变。
有什么建议吗?
答案 0 :(得分:0)
我将if (isset($post_image))
更改为if (!empty($post_image))
并且有效。