如果为空,是否不更改文件上传的数据库值?

时间:2019-04-13 01:08:43

标签: php

我知道下面的代码可以正常工作,我已经尝试过了,并且对于一种输入类型“文件”也可以正常工作,但是如果我有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'";

谢谢。

2 个答案:

答案 0 :(得分:1)

使用var arr_image = [UIImage]() imageView.sd_setImage(with: reference_url, placeholderImage: UIImage(named: "placeholder.jpg"), options: options: [.highPriority]) { (img, error, cachtype, url) in if img != nil{ arr_image.append(img) // append UIimage in array } } 代替name="image[]"name="image1"等。

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

name="image2"

答案 1 :(得分:1)

您要更新数据库中的一行,可能应该将每个图像作为 new 行插入。

例如

$stmt = $pdo->prepare("insert into restaurants set restaurant_name=:name, filename=:name, ...");
$stmt->execute([$restaurantName, $img1]);

standard injection warning applies

每当您拥有img1,img2,img3等列名时,您可能在做错什么。当某些特殊的雪花客户端确定他们确实非常需要一个第三个电话号码时,甚至像电话1和电话2这样的两个重复列也可能早晚会咬你。