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'";
谢谢。
解决方案
使用name="image[]"
代替name="image1"
等name="image2"
。
然后,您可以使用如下字段进行迭代:
foreach($_FILES['image'] as $file) {
if (is_uploaded_file($file)) {
// file was uploaded - do something
}
}
推荐阅读
- php - 如何在 php 中的 html 标记内回显?
- android - 中心操作栏标题 - Android
- python - 限制生成的圆圈数量
- amazon-web-services - 与 tls 1.2 握手后 aws s3 cpp sdk finning 与 minio 的连接
- time-complexity - 你能解释一下为什么这个时间复杂度是 nlog(n)
- python - 使用逗号分隔的输入作为参数
- arrays - 获取二维数组中的多个元素,形状为 (*array)[column]
- c++ - C++ 高斯-赛德尔法
- javascript - 从数组 Typescript 派生类型
- python - 如何在摘要中获取具有 n-gram 重复的文档