3

I need to add a photo update function.

HTML form:

<form action="scripts/setImage.php" method="post"> 
  <h3>Set my image:</h3>
  Choose photo:<br><br>
  <input type="file" name="file"><br>
  <input type="submit" value="Sumbit">      
</form>

PHP:

$file = $_POST['file'];
if (isset($_POST['file'])) {
    $db->UPDATE("UPDATE users SET image = LOAD_FILE('$file') WHERE id = $idLogged");
}

And this doesn't work ;/ I think I can't use $_POST with files. I tried also $_FILES but it doesn't work as well.

5
  • 2
    In the db you only store the name of the imageand or link to the image. If you want to store the image in the db, the field must ba a BLOB type. Normally the image is stored as a file on your server. Commented Dec 10, 2019 at 9:34
  • read this first w3schools.com/php/php_file_upload.asp Commented Dec 10, 2019 at 9:35
  • Add to form enctype='multipart/form-data. Commented Dec 10, 2019 at 9:36
  • And check documentation - php.net/manual/en/features.file-upload.post-method.php Commented Dec 10, 2019 at 9:37
  • Grumpy, it's blow type. And I have images in databases but can't update it Commented Dec 10, 2019 at 9:37

1 Answer 1

5
<form action="scripts/setImage.php" method="post" enctype="multipart/form-data"> 
    <h3>Set my image:</h3>
    Choose photo:<br><br>
    <input type="file" name="file"><br>
    <input type="submit" value="Sumbit">      
</form>

<?php
    $file =addslashes(file_get_contents($_FILES['file']['tmp_name']));
    if (isset($file)) {
        $db->UPDATE("UPDATE users SET image = '$file' WHERE id = $idLogged");
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

I did it and NULL is set to database
Yes, it works. Why did you need to add "addlashes" and "file_get_contents"?
file_get_contents() for get image contents and addlashes for ignore special characters for the time of updating data in table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.