0

我有一个金发的时刻,这是我的问题:

分解:

我有一个 5 张图片的编辑页面,当你点击图片时,它会消失并出现一个上传新图片框。我现在正在为图像代码的更新撞墙。我需要一些帮助来检查图像是否已更改,或者图像是否已被选择删除并删除该图像。

所以它就像这样现有图像>单击以更改或复选框以删除,如果更改则更新新图像并发布回声说图像已更新,如果图像未更改则不执行任何操作或执行某些操作,如果未更改并且已检查删除。

下面的代码块将为每个图像复制:

$image_new  = "test_new_image"; // New file name
$image_hidden  = "test_db_image"; // This is the existing image name in a hidden input
$image_db = "test_db_image"; // Image name in the DB

if ($image_hidden == $image_db) {
    echo "leave image as is in the db<br>";
} elseif ($image_new != $image_db) {
    echo "change image in the db and delete existing image<br>";
} else {
    echo "New image!<br>";
}

或者,如果有人有更好的代码来做我想做的事情会更好。

4

1 回答 1

0
//check if variables are empty, else set them null
$image_new  = (!empty($image_new)) ? $image_new : null;
$image_db  = (!empty($image_db)) ? $image_db : null;

如果此时你知道数据库中的image_name,隐藏字段的值会不会一样?在这种情况下,像这样比较 old_name 和 new_name 就足够了:

//image_new is the same as image_db
if( $image_new === $image_db){
  //leave image as is in the db
}
//there is a new image and it's not the same as image_db
elseif( $image_new !== null && $image_new !== $image_db ){
  //change image in the db and delete existing image (image_db)
}
//there is a new image, and there is nothing in db, yet
elseif( $image_new !== null && $image_db === null ){
  //new image
}
//anything else
else{
  //no image
}
于 2012-07-02T00:35:30.073 回答