Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP multiple image仅上载第一个图像_Php_Mysqli - Fatal编程技术网

PHP multiple image仅上载第一个图像

PHP multiple image仅上载第一个图像,php,mysqli,Php,Mysqli,我正在尝试从一个输入字段“上载”多个图像。我将它们移动到我的“uploads”目录,并将它们的路径保存到我的数据库中。运动部件工作正常,但由于某些原因,它仅存储第一个选定图像的路径,并发出警告消息: mysqli_stmt::bind_param(): Couldn't fetch mysqli_stmt in 守则: $filesTempName = $_FILES['images']['tmp_name']; $counted = count($filesTempName)

我正在尝试从一个输入字段“上载”多个图像。我将它们移动到我的“uploads”目录,并将它们的路径保存到我的数据库中。运动部件工作正常,但由于某些原因,它仅存储第一个选定图像的路径,并发出警告消息:

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->close()到后循环

$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();
            }
        }
    }
}
$filestepname=$\u文件['images']['tmp\u name'];
$counted=count($filestepname);
$maxSize=2100000;
$errorMsg=“”;
如果($counted>5){
$errorMsg=“最多5张图像!”;
}否则{
对于($i=0;$i<$counted;$i++){
if(空($filestepname[$i])){
$stmt->execute();
$stmt->close();
$link->close();
}否则{
$allowed_types=数组(IMAGETYPE_PNG、IMAGETYPE_JPEG、IMAGETYPE_GIF);
$detectedType=exif_imagetype($filesTempName[$i]);
如果($_文件[“图像”][“大小”][$i]>$maxSize){
$errorMsg=“最大2mb!”;
}elseif(!in_数组($detectedType,$allowed_类型)){
$errorMsg=“error!”;
}否则{
$stmt->execute();//我在这里存储其他数据,它工作正常。
$stmt->close();
$productid=$link->insert\U id;
$statement=$link->prepare(“插入图像(thumbnailimage,productid)值(?,)”;
对于($i=0;$i<$counted;$i++){
$file=$filestepname[$i];
如果(是上传的文件($file)&&!空($file)){
$data=“uploads/”.time().$\u文件[“图像”][“名称”][$i];
移动上传的文件($file$data);
$statement->bind_参数(“si”、$data、$productid);
$statement->execute();//我得到此行的错误
}
}
$statement->close();
}
}
}
}

删除
$statement->close()来自您的代码。您不应该手动关闭语句或连接。这同样适用于
$link->close()

您调用
$statement->close()execute()
之后使用code>,然后尝试在第二个循环中使用相同的语句。非常感谢,您是对的。它现在工作得很好。最后一个问题。我应该在我的for循环之后贴上关闭标签吗?
$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();
            }
        }
    }
}