I am trying to add an image in to my database with two columns, name
and id
. However when I tried the code below only the id
is inserted but not the image
. Please tell me where I need to correct the code.
$(function() {
$('#insert').click(function() {
var file = $('#image').val();
$.ajax({
url: "addimg.php",
method: "post",
async: false,
data: {
"insert": 1,
file: file
},
success: function(data) {
$('#image').val('');
}
})
});
});
<input type="file" name="myfile" id="image">
<input type="submit" name="insert" id="insert">
<?php
$conn = mysqli_connect('*****', '****', '*****', '*****');
if (isset($_POST['insert']))
{
$file = addslashes(file_get_contents($_FILES["myfile"]["tmp_name"]));
$query = "INSERT INTO tbl_images(name) VALUES('$file')";
mysqli_query($conn, $query);
}
?>