首页 > 解决方案 > 如果为空,不更改文件上传的数据库值?

问题描述

我知道下面的这段代码可以完美运行,我已经尝试过了,它适用于一种输入类型“文件”,但如果我有 7 个输入类型“文件”怎么办?如何检查其中一个是否为空或全部为空而不将空白值上传到数据库?提前致谢。

if ((!($_FILES['image']['name']))) /* If there Is No file Selected*/ {

"Upload SQL status"

} else /* If file is  Selected*/ {

"Upload SQL status"

$image = $_FILES['image']['name'];
move_uploaded_file($img,"images/$image");
}

如:

<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image1" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image2" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image3" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image4" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image5" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image6" />


<input  type="file" accept="image/x-png,image/jpeg,image/jpg,image/png" 
 name="image7" />

从现场获取图像:

    //getting the image from the field
    $image1 = $_FILES['image1']['name'];


    //getting the image from the field
    $image2 = $_FILES['image2']['name'];


    //getting the image from the field
    $image3 = $_FILES['image3']['name'];


    //getting the image from the field
    $image4 = $_FILES['image4']['name'];


    //getting the image from the field
    $image5 = $_FILES['image5']['name'];

    //getting the image from the field
    $image6 = $_FILES['image6']['name'];

    //getting the image from the field
    $image7 = $_FILES['image7']['name'];

单击更新后,我希望我的其他字段也可以在数据库中更新,如下所示:

$update_restaurant = "update restaurants set 
restaurant_name='$name',restaurant_time='$time', 
area='$area',cuisine='$cuis',header='$header', 
Overview='$overview',Menu='$menu',website='$wb', 
facebook='$fb',meals='$meals',payment='$pay', phone='$phone' , 
map='$map_link', maptext='$map_des', img1='$image1', img2='$image2', 
img3='$image3', img4='$image4',img5='$image5', img6='$image6', 
img7='$image7', mapid='$map_id' where restaurant_id='$update_id'";

谢谢。

标签: php

解决方案


使用name="image[]"代替name="image1"name="image2"

然后,您可以使用如下字段进行迭代:

foreach($_FILES['image'] as $file) {
    if (is_uploaded_file($file)) {
        // file was uploaded - do something

    }
}

推荐阅读