后端模块-管理员登录、显示留言列表
1、今日完成任务:
(1)管理员登录
(2)显示留言列表
2、核心代码
数据库:
-- 创建数据库 BusSystem
drop database if exists BusSystem;
create database BusSystem;
use BusSystem;
-- 创建用户表
drop table if exists UserInfo;
create table UserInfo(
userId int not null PRIMARY key auto_increment,
userTel char(11) not null,
userPass char(8) not null,
nickname varchar(10),
regTime datetime
);
-- 留言表
drop table if exists LeftMessage;
create table LeftMessage(
id int not null PRIMARY key auto_increment,
userId int not null references UserInfo(userId),
content varchar(200),
leftTime datetime
);
-- 管理员表
drop table if exists admin;
create table admin(
id int not null PRIMARY key auto_increment,
adminNo int not null,-- 工号
adminPass varchar(8) not null,
adminName varchar(8)
);
-- 添加输入
insert into admin (adminNo,adminPass,adminName) values(001,'123456','admin');
insert into admin (adminNo,adminPass,adminName) values(002,'123456','root');
select * from admin where adminName="admin" and adminPass="123456";
select * from admin where adminName="root" and adminPass="123456";
-- 添加用户
select * from UserInfo;
insert into UserInfo (userTel,userPass,nickname) values('13678909876','123456','nick');
-- 添加留言
select * from LeftMessage , userinfo where leftmessage.userId=userinfo.userId;
insert into LeftMessage (userId,content,leftTime) values(1,"写什么呢",NOW());
后台代码:
<?php //开启session session_start(); //引入数据库操作方法文件 require_once '../function.php'; //用户登录 if(isset($_POST["username"])){ $username=$_POST["username"]; $pwd=$_POST["pwd"]; $sql="select * from admin where adminName='$username' and adminPass='$pwd';"; $rows=sel($sql); if($rows){ //echo "登录成功"; //把登录信息存储到session $_SESSION["username"]=$username; $_SESSION["pwd"]=$pwd; $autoValue=$_POST["online"]; //保存登录 默认保存7天 if($autoValue=="yes"){ setcookie("auto",1,time()+60*60*24*7);//存储7天 } header("location:index.php"); }else{ //echo "登录失败"; header("location:login.html"); } } ?>
<?php foreach ($rows as $key=>$value){ ?> <tr class="text-c"> <td><input type="checkbox" value="1" name=""></td> <td><?php echo $rows[$key]["userId"] ?></td> <td><a href="javascript:;"><i class="avatar size-L radius" style="line-height:40px;"><?php echo $rows[$key]["nickname"] ?></i></a></td> <td class="text-l"> <div class="f-12 c-999"><?php echo $rows[$key]["content"] ?></div> <div class="c-999 f-12"> <time datetime="<?php echo $rows[$key]["leftTime"] ?>"><?php echo $rows[$key]["leftTime"] ?></time> </div> </td> <td class="td-manage"><a title="编辑" href="javascript:;" onclick="member_edit('编辑','member-add.html','4','','510')" style="text-decoration:none"><i class="Hui-iconfont"></i></a> <a title="删除" href="javascript:;" onclick="member_del(this,'1')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont"></i></a></td> </tr> <?php }?>