Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...