Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update image in database using PHP

Tags:

sql

php

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.

like image 681
programmer001 Avatar asked Dec 06 '25 03:12

programmer001


1 Answers

<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");
    }
?>
like image 128
Piyush Shukla Avatar answered Dec 07 '25 19:12

Piyush Shukla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!