douyan3478 2015-12-14 12:25
浏览 48
已采纳

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

I am trying to save image to my database using mysqli in php. I maded a demo and I used a simple mysqli_query() and it worked. However, when I try to do the same using prepared statement; it doesnt work out. Here is my code:

$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';    
        }

It gives me success however, when I look into my database; I get no image under the image column. I wonder why this happens. May I know the reason why?

  • 写回答

3条回答 默认 最新

  • drhs13583567608 2015-12-17 05:12
    关注

    Using prepared statement, blob was not getting inserted. However, later I tried using simple statement. It worked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog