0

I watched a tutorial on how to do this and still cannot get the image to upload to mysql as a blob.

I want to allow the user to select an image.

    <form action="includes/submitImage.inc.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="image1">
        <button type="submit" name="submit">Save Image</button>
    </form

On submit, it should send this image to mysql db and save it as a blob. db field name is 'selectedImage'.

Contents of submitImage.inc.php:

    <?php 

        include_once 'dbh.inc.php';
        $imageData1 = mysqli_real_escape_string(file_get_contents($_FILES['image1']['tmp_name']));
        $imageType1 = mysqli_real_escape_string($_FILES['image1']['type']);

        $sql = "UPDATE myDbTable SET selectedImage='$imageData1' WHERE id='1';";
        mysqli_query($conn, $sql);
        header("Location: ../home?edit=success");
        exit();
    ?>

This has been watered down to simplify the question. Checks were made that a row with id=1 exists. Code completely makes it through all conditions and url contains 'home?edit=success' as expected. All other fields (that aren't included here) update their values as expected, but the record shows '[BLOB - 0 Bytes]' under the selectedImage field.

Any ideas? I've omitted any code not pertaining to the image and also have changed field names for simplicity. Your time and help are greatly appreciated.

DavidG
  • 785
  • 8
  • 23
  • 1
    remove the header function, see if any error pops up. add `...mysqli_query($conn, $sql) or die($conn->error);` as well – Reinder Wit Jul 15 '18 at 11:09
  • Yes, I got an error for mysqli_real_escape_string() expects exactly two parameters. I believe it is because I don't pass the $conn parameter when getting file info. Trying it now – DavidG Jul 15 '18 at 11:20
  • @ReinderWit that did allow me to get the error that was self-explanatory. I added the $conn parameter (created in dbh.inc.php) to the mysqli_real_escape_string as the first parameter, and it works now. Thanks for the insight. I also didn't know why I wasn't getting errors, but now I know to remove header calls. Thanks. – DavidG Jul 15 '18 at 11:27

1 Answers1

0

Saving Images directly to the database is not a good idea.Move uploaded image file to a safe directory and save it's path or name in the database and retrieve it by reading saved path from database. if you want to do the bad one and save images to the database First be sure that dbh.inc.php file connects to the database correctly.Enable debugging mode to see possible error messages.

RedJS
  • 11
  • 3
  • That's how I normally do it. It is much easier than this in my opinion. I just echo the path to bring the right image and upload the images myself to db. Client wants to select directly and instantly upload the image. Can you please explain why it is a worse method? I'm open to learning. Thanks for your input. – DavidG Jul 15 '18 at 11:25
  • Saving images in the database makes it bigger and bigger and processing uses more CPU than saving images on the filesystem.For some cases it is better to use databases but the best performance depends to your system.look at this page for more information:https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – RedJS Jul 15 '18 at 15:01
  • thanks. Makes sense. I'm gld I tried it this way, but I shall return to my older, easier way of doing as you instruct. save the path and upload the files to the server. Thanks – DavidG Jul 15 '18 at 15:03