运行结果如下:
登录页面。
输入正确的账号密码,成功登录。主页面显示欢迎信息。
进入查询用户页面( query.php)。页面显示所有雇员信息。
数据库设计如下:
loginview.php源代码如下:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>雇员信息系统</title> 5 <meta http-equiv = "content-type" content="text/html; charset = utf-8 "> 6 <!--连接外部样式表wcss.css--> 7 <link rel="stylesheet" href="wcss.css" type="text/css" /> 8 </head> 9 <body bgcolor="#ABC"> 10 <center> 11 <div style="height: 60px; width: 50px"></div> 12 <!--用于调整表单的垂直位置--> 13 <h1 >管理员登录</h1> 14 <form action="loginprocess.php" method="post" > 15 <table> 16 <tr> 17 <th>账号</th> 18 <th><input type="text" name="id"></input></th> 19 <th><div class="error"><?php 20 //div,防止表单错误信息影响布局 21 //判断表单错误信息的存在和显示 22 if (isset($_GET['error'])) { 23 $error=$_GET['error']; 24 }else{ 25 $error=0; 26 } 27 if ($error==1) { 28 echo"*账号或密码错误!"; 29 } 30 ?></div></th> 31 </tr> 32 <!--tr标签内是同一行的内容,th标签内是同一列的内容--> 33 <tr> 34 <th>密码</th> 35 <th><input type="password" name="password"></input></th> 36 </tr> 37 <tr> 38 <th></th> 39 <th> 40 <input type="submit" value="登录"></input> 41 <input type="reset" value="重置"></input> 42 </th> 43 </tr> 44 </table> 45 </form> 46 </center> 47 </body> 48 </html>
loginprocess.php源代码如下:
1 <?php 2 ////设置php文件的编码 3 header("Content-type:text/html;charset=utf-8"); 4 //接受用户数据 5 $id=$_POST['id']; 6 $password=$_POST['password']; 7 //得到连接 8 $conn=mysql_connect("localhost","root","root"); 9 if (!$conn) { 10 die("连接失败!错误信息:".mysql_errno()); 11 } 12 //设置访问数据库的编码 13 mysql_query("set names utf8",$conn) or die("设置编码失败!错误信息:".mysql_errno()); 14 //选择数据库 15 mysql_select_db("aedb",$conn) or die("选择数据库失败!错误信息:".mysql_errno()); 16 //发送SQL语句 17 //ad INNER JOIN em ON ad.id=em.id,检索em和ad表中id相同的所有行。 18 $sql="SELECT password,name FROM ad INNER JOIN em ON ad.id=em.id WHERE ad.id=$id"; 19 //获取查询结果集 20 $res=mysql_query($sql,$conn); 21 //判断查询结果是否存在及是否完全匹配 22 //md5(),计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。 23 if (($row=mysql_fetch_assoc($res)) && $row['password']==md5($password)) { 24 //匹配成功,跳转到mainview.php,并发送管理员姓名 25 //此法默认以GET方法发送信息 26 $name=$row['name']; 27 header("Location:mainview.php?name=$name"); 28 //关闭连接 29 mysql_free_result($res); 30 die(); 31 } 32 //匹配失败,跳转到loginview.php,并发送错误信息error 33 header("Location:loginview.php?error=1"); 34 //关闭连接 35 mysql_free_result($res); 36 die(); 37 ?>
mainview.php源代码如下:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>欢迎使用雇员信息管理系统</title> 5 <meta http-equiv = "content-type" content="text/html; charset = utf-8 "> 6 <!--连接外部样式表wcss.css--> 7 <link rel="stylesheet" href="wcss.css" type="text/css" /> 8 </head> 9 <body bgcolor="#ABC" > 10 <!--混合使用div标签与span标签实现同一行文本不同样式--> 11 <?php 12 if (isset($_GET['name'])) { 13 echo "<div class='welcome'>欢迎<span class='welcome'>".$_GET['name']."</span>登录成功!</div>"; 14 } 15 ?> 16 <hr/> 17 <div class='mainop'> 18 <div><a href="#">管理用户</a></div> 19 <div><a href="#">添加用户</a></div> 20 <div><a href="query.php">查询用户</a></div> 21 <div><a href="#">退出系统</a></div> 22 </div> 23 </body> 24 </html>
query.php源代码如下:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>查询用户</title> 5 <meta http-equiv = "content-type" content="text/html; charset = utf-8 "> 6 <!--连接外部样式表wcss.css--> 7 <link rel="stylesheet" href="wcss.css" type="text/css" /> 8 </head> 9 <body bgcolor="#ABC" > 10 11 <div style="height: 20px"></div> 12 <table class="op"><tr> 13 <th><a href="#">>管理用户</a></th> 14 <th><a href="#">>添加用户</a></th> 15 <th><a href="query.php">>查询用户</a></th> 16 <th><a href="#">>退出系统</a></th> 17 </tr></table> 18 19 <hr/> 20 <h1>查询用户</h1> 21 <?php 22 $conn=mysql_connect("localhost","root","root"); 23 if (!$conn) { 24 die("连接失败!错误信息:".mysql_errno()); 25 } 26 mysql_query("set names utf8",$conn) or die("设置编码失败!错误信息:".mysql_errno()); 27 mysql_select_db("aedb",$conn) or die("选择数据库失败!错误信息:".mysql_errno()); 28 $sql="SELECT * FROM em"; 29 $res=mysql_query($sql,$conn); 30 31 echo "<table border='1px' class='em'><tr><th>id</th><th>name</th></tr>"; 32 while ($row=mysql_fetch_assoc($res)) { 33 echo "<tr><th>".$row['id']."</th><th>".$row['name']."</th></tr>"; 34 } 35 echo "</table>"; 36 37 mysql_free_result($res); 38 ?> 39 </body> 40 </html>
wcss.css源代码如下:
1 table.op{ 2 width:900px; 3 margin:0 auto; 4 font-size: 20px; 5 font-weight:700; 6 text-align:center; 7 } 8 9 h1{ 10 text-align:center; 11 } 12 13 hr{ 14 height:5px; 15 border:none; 16 border-top:5px double blue; 17 } 18 19 div.error{ 20 height: 22px; 21 width: 150px; 22 color:red; 23 size:2px; 24 } 25 26 div.welcome{ 27 color:red; 28 font-size: 50px; 29 font-weight:700; 30 text-align:center; 31 } 32 33 span.welcome{ 34 color:yellow; 35 font-weight:700; 36 size:500px 37 } 38 39 div.mainop{ 40 width:900px; 41 margin:0 auto; 42 font-size: 40px; 43 font-weight:700; 44 text-align:center; 45 } 46 47 table.em{ 48 width:700px; 49 margin:0 auto; 50 }