PHP MySQL 从预先选择的图像中设置个人资料图片

问题描述

我想做一个改变用户头像的功能。我不想上传自定义图像,所以我有地雷。我有一个显示图片的模式,当用户点击图片时,它会将用户导航到 change-prof.PHP用户导航到 PHP 文件后,什么也没有发生。只是一个空白页。

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

PHP 文件

<?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();
    }       
}
?>

解决方法

你的整个代码都写在 if 块下

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

我没有看到这个属性被发布。尝试在此块中添加 else 条件,您会注意到在那里打印的消息。

因此您需要发布此值或将其删除。