为什么这个mysqli语句无法将图像插入数据库


Why is this mysqli statement unable to insert image into the database?

我正在尝试使用php中的mysqli将图像保存到我的数据库中。我做了一个演示,使用了一个简单的mysqli_query(),它成功了。然而,当我尝试使用准备好的语句来做同样的事情时;它不起作用。这是我的代码:

$uploadedImage = array();
        $uploadedImageName = array();
        if(isset($_FILES['files']['tmp_name'])){
            $num_files = count($_FILES['files']['tmp_name']);
            echo $num_files;

            if($num_files == 5){
                for($i = 0; $i < $num_files; $i++){
                    $imgName = addslashes($_FILES['files']['tmp_name'][$i]);
                    $name = addslashes($_FILES['files']['name'][$i]);
                    if($_FILES['files']['tmp_name'][$i] != ""){
                        $imageContents = file_get_contents($imgName);
                        $encodedImage[$i] = base64_encode($imageContents);
                        $filename[$i] = $_FILES['files']['name'][$i];
                        array_push($uploadedImageName, $filename[$i]);
                        array_push($uploadedImage, addslashes($encodedImage[$i]));
                        echo "I got the file..<br>";
                        echo ",,".$filename[$i];
                        echo $encodedImage[$i];
                    }   
                }
            } else{
                echo "Number of files should be equal to 5";
                return;
            }

$sql = "INSERT INTO profile(first_name, middle_name, surname, imgname1, img1, imgname2, img2, imgname3, img3, imgname4, img4, imgname5, img5) VALUES (?, ?, ?,?, ?,?, ?,?, ?,?, ?,?, ?)";
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param("ssssbsbsbsbsb", $firstName, $middleName, $surname, $uploadedImageName[0], $uploadedImage[0], $uploadedImageName[1], $uploadedImage[1], $uploadedImageName[2], $uploadedImage[2], $uploadedImageName[3], $uploadedImage[3], $uploadedImageName[4], $uploadedImage[4]); // bind variables..    

if($stmt->execute()){
            echo 'success';
        } else{
            eccho 'failure';    
        }

然而,当我查看数据库时,它给了我成功;在图像列下没有图像。我想知道为什么会发生这种事。我可以知道原因吗?

你能这样尝试吗,希望它能在中工作

 $stmt = $conn->prepare("INSERT INTO your_table( imgname1) VALUES ( ? )");  //imgname1 is a blob column
 $stmt->bindParam(1, $uploadedImageName[0], PDO::PARAM_LOB);
 $conn->errorInfo();
 $stmt->execute();

您有以下选项:

  1. 您可以尝试在mysql中检查max_allowed_packet的大小。您试图上传的文件可能大于允许的大小。

  2. 您可以尝试使用mysqli_stmt::send_long_data。这对于将Blob保存到数据库非常有用。

  3. 最后,您可能希望使用mysql_stmt::$error来查看任何错误消息,这些消息可以澄清为什么没有插入Blob。

使用prepared语句,blob没有被插入。然而,后来我尝试使用简单语句。它奏效了。