Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

mysqli - PHP multiple image uploads the first image only

I'm trying to 'upload' multiple images from one input field. I move them to my "uploads" directory and save their path into my database. The moving part works fine, but for some reason it stores only the first selected image's path and also gives a warning message:

mysqli_stmt::bind_param(): Couldn't fetch mysqli_stmt in

The code:

    $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();
                        }
                    }
                }
            }
        }
    }

This part throws the error:

$statement->execute();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

move $statement->close(); to after loop

$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();
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...