Edit: My Ajax to send the image data is this
var Image = $('#PostSection').find('input[name="Image2"]').val();
var fd = new FormData();
var files = $('#Image2')[0].files[0];
console.log(files);
fd.append('Image',files);
fd.append('username',username);
fd.append('posts',posts);
fd.append('backText3',backText3);
fd.append('poll',poll);
fd.append('option1',option1);
fd.append('option2',option2);
fd.append('option3',option3);
fd.append('option4',option4);
$.ajax({
url: '../Admin/Posts/Post.php',
type: 'post',
data: fd,
contentType: false,
processData: false,
success: function() {
$('#AllPosts').prepend($('<div>').load(location.href + "
#AllPosts>*"));
$('#PostSection')[0].reset();
var x = document.getElementById("snackbar2");
x.className = "snackbar show";
setTimeout(function(){ x.className = x.className.replace("show", ""); },
3000);
keep in mind I'm only showing the variables for the image since that relates to my question. I don't need to be told about the code for username,posts,poll etc. It's irrelevant to my question.
My image is retrieved like this
$tmpName = $_FILES['Image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$Image = fread($fp, filesize($tmpName));
$Image = addslashes($Image);
fclose($fp);
and my insertion code is like this
$stmt = $conn3->prepare("INSERT INTO `$Username` (Username,RepostedBy,RepostedId,Texts,Img,backText,Dates,TimeUploaded,EditedVersion,Likes,Repost,Tags,Poll)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("ssssbssssssss",$Username,$RepostedBy,$RepostedId,$Texts,$Image,$BackText,$Date,$Time,$EditedVersion,$Likes,$Shares,$Tags,$Poll);
For some reason though, this does not work. My column for my images is longblob. Everything else is inserted, but the image isn't inserted correctly for some reason. A file gets inserted, but it isn't the image. Please note before commenting, as I stated, everything is inserted as it should except that the image isn't inserted properly. I don't need any replies asking me about my connection to my database or if any of my variables are null. Thanks!
The files being inserted are images, and my database says for some of them 129 kib when the image shouldn't be that high in storage. I tried the unprofessional method of doing this
$sql = "INSERT INTO `$Username` (Username,RepostedBy,RepostedId,Texts,Img,backText,Dates,TimeUploaded,EditedVersion,Likes,Repost,Tags,Poll)
VALUES ('$Username','$RepostedBy','$RepostedId','$Texts','$Image','$BackText','$Date','$Time','$EditedVersion','$Likes','$Shares','$Tags','$Poll');";
and this works but it isn't a prepared statement so it isn't ideal at all or professional because I don't want to have SQL injections.