继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

【php增删改查实例】第二十五节 - 在main.php中显示头像

holdtom
关注TA
已关注
手记 1703
粉丝 240
获赞 991

在用户成功上传头像以后,用户登录系统,应该能够看到自己的头像,本节演示如何在这个地方:


添加用户头像。

1.用DIV做:
border-radius:50%
background:url(xxx.jpg)

2.用img标签做:
border-radius:50%
src

共同点:定位

为了演示,先把图片的地址写死。

<img width="64px" height="64px" src="imageUpload\upload/small_Penguins.jpg" />


然后做具体的用户头像查询。

在login2.php中:


当用户登录成功后,我们已经把username存放到了session中。所以,只要用户不去关闭浏览器,用户名username就会保存在session中,又因为username唯一,所以我们在main.php查询头像的时候,不妨根据session中的username去查询。
代码变更:

<img class='header_pic' width="64px" height="64px" src="imageUpload<?php  echo $header; ?>" />

php: (在文件的顶部)

<?php
    session_start();    //在这里查询一下当前用户的头像
    $username = $_SESSION["username"];    //链接数据库
    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db("test",$conn);
    mysql_query("set names utf8");

    $sql = "select header from tm_users where username = '$username';";


    $rs = mysql_query($sql);

    $header = null; //现在外面定义一下头像变量,否则在while循环体中就是局部变量了,外面访问不到。

    while($row = mysql_fetch_array($rs)){

        $header = $row["header"];


    }?>

设置默认头像

在实际的开发中,如果当前用户还没有设置头像,那么系统一般会给这个用户设置一个默认头像。
在本系统中,我们就判断select header from tm_users where username = '$username';查询出来的头像是否为空,如果为空,就给他一个默认的头像:

image.png


地址为:imageUpload\images\header.png

源码获取:https://www.jianshu.com/p/4977bd0073d5



作者:剽悍一小兔
链接:https://www.jianshu.com/p/500e9a69ecc0

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP