将图像插入数据库而不使用表单提交AJAX PHP

时间:2018-05-29 20:27:31

标签: php jquery mysql ajax slim

我试图从类型='文件中插入图片'使用AJAX将HTML标记到MySQL数据库中。

我刚接触使用AJAX进行图片上传,我觉得我的语法错误。

我已经对此做了一些研究,我想我已经接近了。

HTML:

 <input type="file" id="HTMLmyimage"> 

AJAX:

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



$.ajax({
        type: "POST",
        enctype: 'multipart/form-data',
        processData: false, // important
        contentType: false, // important
        url: "PHPURL",
        data: { 
            'BEimage': $form_data,
        },
        success: function(result) {
            alert('User Added')
        },
        error: function(result) {
            alert('error');
        }
    });

PHP:

$image = $request->getParam('BEimage');

$imageData = (file_get_contents($_FILES[$image]['tmp_name']));

$sql4 = "INSERT INTO customerlocation (CustomerID, Username, img) VALUES ('$CustomerID', '$Username', :img)";
     $stmt4 = $db->prepare($sql4);  
     $stmt4->bindParam(':img', $imageData);
     $stmt4->execute();

我当前的错误是说$ image没有定义,但我不确定我是否在做正确之前做了什么。

对此的任何帮助都很棒:)

谢谢

0 个答案:

没有答案
相关问题