简体   繁体   中英

How to Show Profile Picture of User on Change password form (Picture of Logged In user should be show) in HTML/PHP

I'm working on a little project, I've created and linked change password form and used update query, form is working very well, but I want to show user's image on the change password form, Below is my code, when I used session variable and add php code in form, then my already fixed image also become hidden whereas, I don't want to show fixed image I want to show user's image, Please help me to correct it. I used if else and switch statement but only else statement is executing.

<div class="container">
    <h1>CHANGE YOUR PASSWORD</h1>
    <div class="contact-form">
        <div class="profile-pic">
<?php
if (isset($_SESSION['username'])){
    $username = !empty($_SESSION['username']) ? $_SESSION['username'] : false;
    switch ($username) {
        case 'admin':
        case 'Admin':
        case 'ADMIN':
?>
            <img src="profile_pics/4.png" alt="User Icon"/>
<?php
            break;
        case 'muhammad azeem':
        case 'Muhammad Azeem':
        case 'MUHAMMAD AZEEM':
?>
            <img src="profile_pics/1.png" alt="User Icon"/>
<?php
            break;
        case 'muhammad adnan':
        case 'Muhammad Adnan':
        case 'MUHAMMAD ADNAN':
?>
            <img src="profile_pics/2.png" alt="User Icon"/>
<?php
            break;
        case 'saleem raza':
        case 'Saleem Raza':
        case 'SALEEM RAZA':
?>
            <img src="profile_pics/3.png" alt="User Icon"/>
<?php
            break;
        case 'abdul raheem':
        case 'Abdul Raheem':
        case 'ABDUL RAHEEM':
?>
            <img src="profile_pics/5.png" alt="User Icon"/>
<?php
            break;
    }
} else{
?>
            <img src="images/1.png" alt="User Icon"/>
<?php
}
?>
        </div>
        <div class="signin">
            <form action="process_change_password_form.php" method="POST">
                <input type="text" name="current_password" class="user" 
                        value="Current Password" 
                        onfocus="this.value = '';" 
                        onblur="if (this.value == '') {this.value = 'Current Password';}" 
                        id="1" required>
                <input type="text" name="new_password" class="user" 
                        value="New Password" 
                        onfocus="this.value = '';" 
                        onblur="if (this.value == '') {this.value = 'New Password';}" 
                        id="1" required>
                <input type="text" name="confirm_new_password" class="user" 
                        value="Confirm New Password" 
                        onfocus="this.value = '';" 
                        onblur="if (this.value == '') {this.value = 'Confirm Password';}" 
                        id="1" required>
                <input type="submit" value="Change Password" name="submit" />
            </form>
        </div>
    </div>
</div>
</body>
</html>

You have to change yours switch statement with:

<?php
$img = '1.png';  //Default image
switch (strtolower($username)) {
    case 'admin':
        $img = '4.png';
        break;
    case 'muhammad azeem':
        $img = '1.png';
        break;
        ...
 }
 echo '<img src="profile_pics/'.$img.'" alt="User Icon"/>';
?>

strtolower() convert all characters to lowercase so you can check $username only one time for case.

Anyway, I suggest you to remove the switch function and save profile image file in a session variable and then use simply

echo '<img src="profile_pics/'.$_SESSION['img_file'].'" alt="User Icon"/>';

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