duanbu1998 2015-11-09 19:27
浏览 120

jQuery上传并通过PHP将图像插入MySQLi BLOB

I am having trouble with uploading an image into MySQL and saving it into BLOB format. I am aware that there are different ways to save images but for this project I need to do it this way.... Here is the code I am trying with:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">  
    $(document).ready(function(){
    $('#submit_profile').on('click', function() {
        $.ajax({
            type: "POST",
            url: 'updateProfile.php',
            dataType: "html",
            data: {
                //that way it just dont work
                //I've seen some examples where they use
                // var formData = new FormData($('#updateProfile'));
                userID: <?php echo $_SESSION['userID']; ?>,
                image: $('#image').val(), // This didn't work
                city: $('select[name=city]').val(),
                desc: $('#desc').val()
            },
            success: function(data) {
                $('.resultDiv').html(data);
            }
        )};
    )};
)};
</script>
</head>
<body>
<form method="POST" id="updateProfile" enctype="multipart/form-data">
Image: <input type="file" name="image" id="image" accept="image/*"><br>
City: <select name="city">
        <option value="Moscow">Moscow</option>
        <option value="New York">New York</option>
        </select>
Description: <textarea name="desc" id="desc"></textarea>
</form>
<button id="submit_profile" class="submit">Update</button>
</body>
</html>

The PHP side:

<?php
session_start();
require ("db.php");

if($_POST['userID'] == $_SESSION['userID']) {
    $userID = $_POST['userID'];
    // Image
    $city = trim(mysqli_escape_string($conn, $_POST['city']));
    $desc = trim(mysqli_escape_string($conn, $_POST['desc']));

    $errorinfo = $_FILES["userImage"]["error"];
    $filename = $_FILES["userImage"]["name"];
    $tmpfile = $_FILES["userImage"]["tmp_name"];
    $filesize = $_FILES["userImage"]["size"];
    $filetype = $_FILES["userImage"]["type"];

    //
    if (!($filetype == "image/jpeg" && $filesize > 0)) {
       echo "<script>alert('Import of photo failed')</script>";
    } else  if($filesize < 0 && $filesize > 5120) {
        echo "<script>alert('Import of photo failed!')</script>";
    } else {

        // Update query
    }
} else {
    echo "<script>window.location='/index.php';</script>";
}
?>

Can someone give me an working example that fits my needs. Thank you in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?