php之使用者管理系統的實現!(從簡單到複雜)

科技小能手發表於2017-11-12

規劃圖:

image

一、資料庫的設計:


資料庫的建立: create database empmanage;


管理員表Admin

create table admin(

Adm_id int primary key,

Adm_name varchar(32) not null,

Adm_password varchar(128) not null );


僱員表:emp

create table emp(

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`));

image

二、登陸的實現:


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>&nbsp;</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 登入成功;

測試結果:

登陸成功的話:

image

image

故意輸錯,在下面出現提示!

image

第2種方法:到資料庫驗證:

image

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);


測試結果:

先看下資料庫中的管理員表:

image

image

image


三、如何出現,歡迎某個使用者登入的介面!


實現方法,用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`].登入成功;

image


四、現在實現:整體設計

image

這裡的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>

image

image

image

image

image

image

image

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 “.($pageNow1)*$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>

image


參考傳智播客,做的筆記及其測試!

後面會有mvc的實現模式!(敬請關注!!!)

本文轉自 gjp0731 51CTO部落格,原文連結:

http://blog.51cto.com/guojiping/1350962


相關文章