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!