1

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!

diank
  • 628
  • 2
  • 11
  • 19
  • 1. Get the file's contents. 2. Put that in the DB. 3. Go back to #1 and store the file as a file and just put the file's location in the database instead. – Sammitch Nov 09 '15 at 19:31
  • 1
    I am aware of that method , but I need the images to be stored in BLOB format in MySQL via Ajax call. – diank Nov 09 '15 at 19:49
  • 1
    BLOB isn't a format, it's a field type denoting that arbitrary binary data is stored inside. I now refer you to numbers 1 and 2 of my initial comment. – Sammitch Nov 09 '15 at 21:07
  • 1
    Does this help http://stackoverflow.com/a/1638348 – sammry Nov 09 '15 at 21:08
  • Ajax upload files with [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects) object. – gre_gor Nov 09 '15 at 21:59

0 Answers0