简体   繁体   中英

How to update the user profile image using session?

I want to update my profile image but I don't know how to update it. I successfully insert it into my database and folder but when user wants to update it So I don't know how to update this image. Can you tell me how this is possible? Thanks.

Code of my HTML form

<form class="form" action="accountsetting.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-12 col-sm-auto mb-3">
<div class="mx-auto" style="width: 140px;">
<div class="rounded-circle avatar avatar-xl mb-3">
<img class="rounded-circle" id="preview_avatar" name="avatar" src="<?php echo 
$_SESSION['user']['avatar']; ?>"  width="100" height="100">
</div>
</div>
</div>
<div class="col d-flex flex-column flex-sm-row justify-content-between mb-3">
<div class="text-center text-sm-left mb-2 mb-sm-0">
<h4 class="pt-sm-2 pb-1 mb-0 text-nowrap"><?php echo $_SESSION['user']['fullname']; ?></h4>
<p class="mb-0"><?php echo $_SESSION['user']['username']; ?></p>
<div class="form-group mb-2 pt-2">
<input class='input' type='hidden' name='id' value="<?php echo $_SESSION['user']['id']; ?>" />
<input id="avatar" type="file" name="avatar" hidden="" accept="image/png, image/jpeg, 
image/jpg">
<button id="uploadBtn" type="submit" name="profile" class="btn btn-primary btn-file " ><i 
class="fa fa-camera" aria-hidden="true"></i> &nbsp Upload Avatar</button>
</div>
<div class="row">
<div class="col d-flex justify-content-end pr-5">
<button class="btn btn-primary " type="submit" name="profileupdate">Save Changes</button>
</div>
</div>
</div>
</div>
</div>
</form>

Code of my PHP side

// sava image into directoray
$id="";
$filename = $avatar['name'];
$fileerror = $avatar['error'];
$filetmp = $avatar['tmp_name'];
$fileext = explode('.',$filename);
$filecheck = strtolower(end($fileext));
$fileextstored =array('png', 'jpg', 'jpeg');
$destinationfile = 'upload/'; 
$destinationfile = $destinationfile .$filename;
$destinationfile1 = 'userside/upload/'; 
$destinationfile1 = $destinationfile1 .$filename;
try {
//throw exception if can't move the file
if (!move_uploaded_file($filetmp, $destinationfile)) {
throw new Exception('Could not move file');
}
if(!copy ( $destinationfile , $destinationfile1 ))
    {  
      throw new Exception('Could not move 2nd file');
    }
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}

// image insert
if(in_array($filecheck, $fileextstored))
{
$query = "INSERT INTO users (avatar) VALUES('$destinationfile')";
   $displayquery = "select * from users where id= $id";
   $displayquery = mysqli_query($db,$displayquery);  
}

What's type of code i will use. When the user click on the save button the image will successfully update.

It looks like you need a condition in your INSERT INTO statement. Shouldn't you insert the avatar in the users table where id = $id?

Here:

"INSERT INTO users (avatar) VALUES('$destinationfile')";

Change to:

"INSERT INTO users (avatar) VALUES('$destinationfile') WHERE id = $id";

Update: if you're inserting the path to the image, then maybe you need to put the URL in with the image tag:

<!-- insert code for http or https:// -->
<img class="rounded-circle" id="preview_avatar" name="avatar" src="<?php  echo "http://" . $_SERVER['SERVER_NAME'] . "/" . $_SESSION['user']['avatar']; ?>"  width="100" height="100">

This relies on that path being valid and accessible from the server name, like http://localhost/uploads/image_name.jpg if the path is.../uploads/image_name.jpg. You can put the path in the session variable, then just add the image name on the end, eg $_SESSION['image_path'] = 'http://localhost/uploads/'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM