phpmysqluser-profileprofile-picture

PHP MySQL Set Profile Picture From Pre-Selected Images


I want to make a function that changes the user's profile picture. I don't want to upload custom images, so I have mines. I have a modal, which is displaying the pictures, and when the user click onto the image, it navigates the user to change-prof.php. After the user gets navigated to php file, nothing happens. Just a blank page.

 <a href='change-prof.php?id={$id}'>
    <div class='prof-img-con'>
        <img src='/assets/images/profiles/smile.jpg'>
    </div>
</a>

The PHP file:

<?php

$db = mysqli_connect("localhost", "root", "", "phplogin");
if(!$db)

{

  die("Connection failed: " . mysqli_connect_error());

}
$id = $_GET['id'];
$qry = mysqli_query($db,"SELECT profile_picture FROM accounts WHERE id='$id'");

$data = mysqli_fetch_array($qry);
if (isset($_POST['suspendok'])) {
    $smile= '/assets/images/profiles/smile.jpg';

    $edit = mysqli_query($db,"UPDATE accounts SET profile_picture='$smile' WHERE id='$id'");
    
    if($edit)
    {
        mysqli_close($db); // Close connection
        header("location: index.php?change=success");
        exit;
    }
    else
    {
        echo mysqli_error();
    }       
}
?>

Solution

  • Your whole code is written under if block

    if (isset($_POST['suspendok'])) {
    

    I dont see this property getting posted. Try adding else condition to this block you will notice the message being printed there.

    So you need to either post this value or remove it.