mysql - PHP/MYSL - creating avatar upload function for users using BLOB -
i creating forum , trying allow users able update profile picture (avatar). attempting blob. aware storing images on database not idea self-learning project , unlikely considered real life use in future.
i coming across issue of image not being saved in database , resulting in following error:
notice: undefined index: user_avatar in c:\wamp64\www\latest_try\editprofile.php on line 24
i newb in php (clearly) , appreciate if me understand need make save image database.
i wonder if can save image or need complete seperate table somehow link users information using foreign key.
below mysql table using in relation function , issue.
mysql -> describe users; +-----------------+-------------+------+-----+---------+--------------+ | field | type | null | key | default | | +-----------------+-------------+------+-----+---------+--------------+ | 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 code editprofile.php file.
<?php include 'connect.php'; include 'header.php'; if(!isset($_session['signed_in'])) { //the user not logged in. echo 'you must <a href="/latest_try/signin.php">signed in</a> add image 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 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!'; } } } ?>
as know, saving images in db bad practice. however, issue having due incorrect variable used. check below statement:
$query = "insert users(user_avatar) values ('" . mysqli_real_escape_string($link, $_files['user_avatar']['name']) . "', now(), '" . $_session['user_id'] . "')";
you can refer more here.
Comments
Post a Comment