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

Answer

Solution:

Remove $statement->close(); from your code. You should not be closing the statement or the connection manually. Same goes for $link->close();.

Answer

Solution:

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

Source