Search code examples
phpmysqlblobavatar

PHP/MySQL - creating avatar upload function for users using BLOB


I am creating a forum and am currently trying to allow my users to be able to update their profile picture (avatar). I am attempting to do this with BLOB. I am aware that storing images on a database is not a good idea but this is just a self-learning project and will unlikely be considered for real life use in the future.

I am currently coming across the issue of the image not being saved in the database and resulting in the following error:

Notice: Undefined index: user_avatar in C:\wamp64\www\Latest_try\editprofile.php on line 24

I am a newb in php (clearly) and would appreciate if someone could help me understand what i need to make it save the image to the database.

I also wonder if i can save the image like this or would i need a complete seperate table that could then somehow link to the users information using a foreign key.

Below is mysql table i am using in relation to the function and the issue.

mysql -> describe users;
+-----------------+-------------+------+-----+---------+--------------+
| Field           | Type        | Null | Key | Default | Extra        |
+-----------------+-------------+------+-----+---------+--------------+
| user_Id         | int(8)      | NO   | PRI | NULL    |auto_increment|
| user_name       | varchar(30) | NO   |     | NULL    |              |
| user_pass       | varchar(255)| NO   |     | NULL    |              |
| user_email      | varchar(255)| NO   |     | NULL    |              |
| user_date       | datetime    | NO   |     | NULL    |              |
| user_level      | int(8)      | NO   |     | NULL    |              |
| user_description| varchar(255)| YES  |     | NULL    |              |
| user_avatar     | longblob    | NO   |     | NULL    |              |
+-----------------+-------------+------+-----+---------+--------------+
8 rows in set (0.02 sec)

And here is my code for editprofile.php file.

<?php
include 'connect.php';
include 'header.php';

if(!isset($_SESSION['signed_in']))
{
//the user is not logged in.
echo 'You must be <a href="/Latest_try/signin.php">Signed in</a> to add an image to your profile.'; 
}
else
{
if($_SERVER['REQUEST_METHOD']!='POST')
{
echo '<form action="" method="post" enctype="multipart/form-data">
Choose Image: <input type="file" name="user_avatar"><br/>
<input type="submit" name="submit" value="Upload">
</form>';
}
else
{
    $query = "INSERT INTO
                        users(user_avatar)
                    VALUES
                        ('" . mysqli_real_escape_string($link, $_POST['user_avatar']) . "',
                              NOW(),
                              '" . $_SESSION['user_id'] . "')";

$result=mysqli_query($link, $query);
if($result===false)
{
    mysqli_error($link);
}
     else
     {
         echo 'Your avatar has been updated!';
     }      
}
}
?>

Solution

  • As you already know, saving images in the DB is a bad practice. However, the issue you are having is due to incorrect variable used. Check the below statement:

    $query = "INSERT INTO
              users(user_avatar)
              VALUES
              ('" . mysqli_real_escape_string($link, $_FILES['user_avatar']['name']) . "',
              NOW(),
              '" . $_SESSION['user_id'] . "')";
    

    You can also refer more about this here.