php - PHP多张图片只上传第一张图片
问题描述
我正在尝试从一个输入字段“上传”多个图像。我将它们移动到我的“上传”目录并将它们的路径保存到我的数据库中。移动部分工作正常,但由于某种原因,它只存储第一个选定图像的路径,并给出警告消息:
mysqli_stmt::bind_param(): Couldn't fetch mysqli_stmt in
编码:
$filesTempName = $_FILES['images']['tmp_name'];
$counted = count($filesTempName);
$maxSize = 2100000;
$errorMsg = "";
if ($counted > 5) {
$errorMsg = "Maximum 5 images!";
} else {
for ($i = 0; $i < $counted; $i++) {
if (empty($filesTempName[$i])) {
$stmt->execute();
$stmt->close();
$link->close();
} else {
$allowed_types = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($filesTempName[$i]);
if ($_FILES["images"]["size"][$i] > $maxSize) {
$errorMsg = "max 2mb!";
} elseif (!in_array($detectedType, $allowed_types)) {
$errorMsg = "error!";
} else {
$stmt->execute(); //I store other data here, it works fine.
$stmt->close();
$productid = $link->insert_id;
$statement = $link->prepare("INSERT INTO images(thumbnailimage, productid) VALUES(?, ?)");
for ($i = 0; $i < $counted; $i++) {
$file = $filesTempName[$i];
if (is_uploaded_file($file) && !empty($file)) {
$data = "uploads/" . time() . $_FILES["images"]["name"][$i];
move_uploaded_file($file, $data);
$statement->bind_param("si", $data, $productid);
$statement->execute(); // I get error for this line
$statement->close();
}
}
}
}
}
}
这部分抛出错误:
$statement->execute();
解决方案
$statement->close();
从您的代码中删除。您不应该手动关闭语句或连接。也一样$link->close();
。
推荐阅读
- winapi - WinAPI Slim Reader/Writer (SRW) 锁是否使用内存屏障?
- php - php条件过滤json数据的结果
- javascript - 我应该如何在单击时将复选框值设置为 false?
- list - 当其他一些小部件在颤动中时,如何防止渲染给定的小部件?
- rest - 无法使用路径变量调用 GET REST URL
- c++11 - 如何让用户定义移动构造函数调用
- dart - 如何将csv数据存储在列表中
- python - 如何从其他列的每个组合的列中找到最大值
- php - 在购物车页面 woocommerce 中显示 div 内联
- javascript - __dirname 未在 NodeJS SocketIO 中定义