規劃圖:php
1、數據庫的設計:html
數據庫的建立: create database empmanage;mysql
管理員表Admin sql
create table admin(數據庫
Adm_id int primary key,mvc
Adm_name varchar(32) not null,ide
Adm_password varchar(128) not null );post
僱員表:emp測試
create table emp(fetch
Emp_id int primary key auto_increment,
Emp_name varchar(64) not null,
Emp_grade tinyint,
Emp_email varchar(64) not null,
Emp_salary float
);
添加點數據表裏:
insert into emp (Emp_name,Emp_grade,Emp_email,Emp_salary) values ('jiping',1,'gjp@lonlife.net',3000);
添加多條,做爲後面的分頁使用!
insert into admin values(100,'admin',md5('admin'));
2、登錄的實現:
第1種方法:簡單的實現,不到數據庫驗證!
第一個界面:login.php
<html>
<head>
<metahttp-equiv="content-type"content="text/html;charset=utf-8">
</head>
<h1>管理員登錄系統</h1>
<formaction="loginprocess.php"method="post">
<table>
<tr><td>用戶id</td><td><inputtype="text"name="id"/></td></tr>
<tr><td>密 碼</td><td><inputtype="password"name="password"/></td></tr>
<tr>
<td><inputtype="submit"value="用戶登陸"/></td>
<td><inputtype="reset"value="從新填寫"/></td>
</tr>
</table>
</form>
<?php
if(!empty($_GET['errno'])){
$errno=$_GET['errno'];
if($errno=='1'){
echo "<br/><font color='red' size='3'>你的用戶名和密碼有誤";
}
}
?>
</html>
第二個界面:loginprocess.php
<?php
//接收用戶的數據
$id=$_POST['id'];
$password=$_POST['password'];
if($id=="100" && $password=="123"){
//合法:跳轉到empManage.php
header("Location: empManage.php");
exit();
}else {
header("Location: login.php?errno=1");
}
第三個界面:empManage.php
<?php
echo "登陸成功";
測試結果:
登錄成功的話:
故意輸錯,在下面出現提示!
第2種方法:到數據庫驗證:
第2種方法的實現:loginprocess.php
<?php
//接收用戶的數據
$id=$_POST['id'];
$password=$_POST['password'];
//2.到數據庫進行驗證
$conn=mysql_connect("localhost","root","123456");
if(!$conn)
{
die("鏈接失敗".mysql_errno());
}
mysql_query("set names utf8",$conn) or die(mysql_errno());
mysql_select_db("empmanage",$conn) ordie(mysql_errno());
$sql="select Adm_password from admin where Adm_id=$id";
$res=mysql_query($sql,$conn);
if($row=mysql_fetch_assoc($res))
{
//查詢到id,下面要比對密碼
if($row['Adm_password']==md5($password))
{
header("Location: empManage.php");
exit();
}
}
header("Location: login.php?errno=1");
exit();
//關閉資源
mysql_free_result($res);
mysql_close($conn);
測試結果:
先看下數據庫中的管理員表:
3、如何出現,歡迎某個用戶登陸的界面!
實現方法,用Get 方式傳遞
(注意:數據庫的選取字段)loginprocess.php
$sql="select Adm_password,Adm_name from admin where Adm_id=$id";
$res=mysql_query($sql,$conn);
if($row=mysql_fetch_assoc($res))
{ //查詢到id,下面要比對密碼
if($row['Adm_password']==md5($password))
{$name=$row['Adm_name'];
header("Location: empManage.php?name=$name");
exit();
}
}
empManage.php
<?php
echo "歡迎".$_GET['name']."登陸成功";
4、如今實現:總體設計
這裏的empMain.php由 empManage.php來代替
<html>
<head>
<metahttp-equiv="content-type"content="text/html;charset=utf-8">
</head>
<?php
echo "歡迎".$_GET['name']."登陸成功";
echo "<br/><a href='login.php'>返回登錄界面";
?>
<h1>主界面</h1>
<ahref='emplist.php'>管理用戶</a><br/>
<ahref='#'>添加用戶</a><br/>
<ahref='#'>查詢用戶</a><br/>
<ahref='#'>退出系統</a><br/>
</html>
Emplist.php 頁面代碼
<html>
<head>
<metahttp-equiv="content-type"content="text/html charset=utf-8">
</head>
<title>僱員信息列表</title>
<?php
//顯示全部用戶的信息(表格)查詢數據,在數據庫
$conn=mysql_connect("localhost","root","123456");
if(!$conn)
{
die("鏈接失敗".mysql_errno());
}
mysql_query("set names utf8",$conn) or die(mysql_errno());
mysql_select_db("empmanage",$conn) ordie(mysql_errno());
$pageSize=3;
$rowCount=0;
$pageNow=1;//若是沒有傳送這個參數的話,默認值爲1
if(!empty($_GET['pageNow']))
{ $pageNow=$_GET['pageNow'];}
$pageCount=0;
$sql="select count(Emp_id) from emp";
$res1=mysql_query($sql);
if($row=mysql_fetch_row($res1))
{$rowCount=$row[0];
}
$pageCount=ceil($rowCount/$pageSize);
/*if($rowCount%$pagesize==0){
$pageCount=$rowCount/$pagesize;
}else{
$pageCount=ceil($rowCount/$pagesize);
}*/
$sql="select * fromemp limit ".($pageNow-1)*$pageSize.",$pageSize";
$res=mysql_query($sql,$conn);
echo "<table border='1px' bordercolor='green' cellspacing='0px' ;";
echo "<tr><th>Emp_id</th><th>Emp_name</th><th>Emp_grade</th><th>Emp_email</th><th>Emp_salary</th><th>修改用戶</th><th>刪除用戶</th></tr>";
//循環顯示用戶的信息
while($row=mysql_fetch_assoc($res))
{
echo"<tr><td>{$row['Emp_id']}</td><td>{$row['Emp_name']}</td><td>{$row['Emp_grade']}</td><td>{$row['Emp_email']}</td><td>{$row['Emp_salary']}</td>".
"<td><a href='#'>修改用戶</td><td><a href='#'>刪除用戶</td></tr>";
}
echo "<h1>僱員信息列表</h1>";
echo "</table>";
//打印出頁面的超連接
for($i=1;$i<=$pageCount;$i++)
{echo "<a href='emplist.php?pageNow=$i'>$i$nbsp ";
}
mysql_free_result($res);
mysql_close($conn);
?>
</html>
參考傳智播客,作的筆記及其測試!
後面會有mvc的實現模式!(敬請關注!!!)