0

I cannot update an image in my database and I never received any errors while running this. My html is for users to upload a file, my PHP is to update the file, and my js it to verify the file. THIS HAS BEEN SOLVED AND IT TURNED OUT THAT MY INPUT NAME IS SUPPOSED TO BE file-input NOT image.

PHP:

if(isset($_POST["insert"]))
{
     $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
     $query = "UPDATE users SET image= ? WHERE email= ?";
     $stmt=$link->prepare($query);
     $stmt->execute([$file, $param_email]);
}

HTML:

<form method="post" enctype="multipart/form-data">
                 <?php
                 $query = $link->prepare('SELECT image FROM users WHERE email= ?');
                 if ( !$query ) {
                     yourErrorHandler($link->error); // or $mysqli->error_list
                 }
                 else if ( !$query->bind_param('s', $param_email)) {
                     yourErrorHandler($stmt->error); // or $stmt->error_list
                 }
                 else if ( !$query->execute() ) {
                     yourErrorHandler($stmt->error); // or $stmt->error_list
                 }
                 else {
                     $result = $query->get_result();
                     foreach( $result as $row ) {
                       echo ' <div class="image-upload">
                                     <label for="file-input">
 <img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="200" width="200" class="img-thumnail" />
         </label>
         <input type="file" name="file-input" id="file-input" />
     </div>

Js:

$(document).ready(function(){
        $('#insert').click(function(){
             var image_name = $('#file-input').val();
             if(image_name == '')
             {
                  alert("Please select an image");
                  return false;
             }
             else
             {
                  var extension = $('#file-input').val().split('.').pop().toLowerCase();
                  if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
                  {
                       alert('Invalid Image File');
                       $('#file-input').val('');
                       return false;
                  }
             }
        });
   });

*My database has image as mediumblob, default is none, and its not null

PHP noob
  • 11
  • 4

2 Answers2

4

You use $_FILES["image"]["tmp_name"] instead $_FILES["file-input"]["tmp_name"]

The input name is file-input and not image

R3tep
  • 12,512
  • 10
  • 48
  • 75
-1

I think you need to bind the parameters first:

if(isset($_POST["insert"]))
{
 $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
 $query = "UPDATE users SET image= ? WHERE email= ?";
 $stmt=$link->prepare($query);
 $stmt->bindParam(1, $file, PDO::PARAM_STR);
 $stmt->bindParam(2, $param_email, PDO::PARAM_STR);
 $stmt->execute();
}
michal
  • 327
  • 4
  • 15