dougourang1856 2016-08-26 06:34
浏览 78

使用base64编码数据更新SQL查询

For one my application, I need to keep image files in database table. I am using AJAX to send the multi-part form data and can view the image file details on action page.

On action page, base64 encoding is working and showing the correct encoded data. Also, I saw the encoded data is showing on UPDATE SQL query by echoing the SQL statement.

But while trying to execute the SQL query statement then every time its only inserting data:image/;base64, and ignoring the base64 encoded full data.

When I submit the form without using AJAX then the system update the database table with base64 encoded data.

Sample code: JavaScript:

$(document).on("change", "#bird", function() {

  var file_data = $("#bird").prop("files")[0]; 
  var form_data = new FormData(); 
  form_data.append("file", file_data)

  $.ajax({
        type: 'post',
        contentType: false,
        processData: false,
        data: image_data, 
        url: "/update-bird.php", 
        dataType: 'json',
        cache:false,
        success: function(data) {
          // Ajax             
        }
   });
});

PHP:

if(isset($_FILES))
{
    $path = $_FILES['file']['name'];
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($_FILES['file']['tmp_name']);

    $birdpic = "data:image/".pathinfo($path, PATHINFO_EXTENSION).";base64,".base64_encode(file_get_contents($_FILES['file']['tmp_name']));

    $updateQ = "UPDATE birds SET picture='$birdpic' WHERE name='Yoy'";

    if($dbo->query($updateQ))
        echo 1;
    else
        echo 0;
 }

I hope, will get some advice to solve this issue. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dousao6313 2016-08-26 07:13
    关注

    Try this one - Table column must be BLOB or LONGBLOB

    $img_data = file_get_contents($img_path);
    $type = pathinfo($img_path, PATHINFO_EXTENSION);
    $birdpic = base64_encode($img_data);
    $updateQ = "UPDATE birds SET picture='$birdpic' WHERE name='Yoy'";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料