dongxi2163 2016-03-08 14:46
浏览 37
已采纳

尝试上传多个图像到数据库无法正常工作

I am trying to upload images to database using prepared statement. Whenever I execute the query the fields name and id are inserted correctly in database as expected except the images I am getting 0 bytes in database. I tried uploading multiple images using move_uploaded_file() and it worked but it just didn't with database I tried uploading an image of only size 10kb and didn't work. its keep giving me 0 byte in database.

here is my code

//uploading image to database
function upload_image($last_id, $conn)
{
    $extension = [
        "jpeg",
        "jpg",
        "png",
        "gif",
    ];
    if (isset($_POST["submit"])) {
        //in case of uploading multiple images
        foreach ($_FILES["input"]["tmp_name"] as $key => $tmp_name) {
            $name = basename($_FILES["input"]["name"][$key]);
            $imageFileType = pathinfo($name, PATHINFO_EXTENSION);

            if (in_array($imageFileType, $extension)) {
                $stmt3 = $conn->prepare("INSERT INTO image (id ,name, image) VALUES (?, ?, ?)");
                $image = addslashes(file_get_contents($_FILES["input"]["tmp_name"][$key]));
                $stmt3->bind_param("isb", $last_id, $name, $image);
                $stmt3->execute();
                $stmt3->close();
            }
        }//end loop
    }//end if
}//end function

there is no need to put the html code of the form since it already worked with move_uploaded_file()

  • 写回答

1条回答 默认 最新

  • douyabu1528 2016-03-12 18:10
    关注

    i only replaced the type of data of the $image in the bind_param from blob 'b' to string 's' and it works.

    $stmt3->bind_param("iss", $last_id, $name, $image);
    

    no idea why it didn't work the other way

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题