亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 40
粉絲 2
評(píng)論 1
訪問(wèn)量 45931
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
4.PHP后臺(tái)管理系統(tǒng)(增刪查改分)
萬(wàn)物皆對(duì)象
原創(chuàng)
5719人瀏覽過(guò)

初始判斷: is_session.php

<?php
	// 判斷是否存在session 意味著已登錄就存在,否則不跳轉(zhuǎn)到登錄頁(yè)面
	session_start();
	if($_SESSION['user_id'] && $_SESSION['username']){
		define('DSN','mysql:host=127.0.0.1;dbname=php;charset=utf8');
		define('USER','root');
		define('PWD','root');
		try{
			$pdo = new PDO(DSN,USER,PWD);
		}catch(PDOException $e){
			echo "數(shù)據(jù)庫(kù)連接失敗! ".$e->getMessage();
			die;
		}	
	}else{
		header('location:login.php');
	}
?>


登錄頁(yè)面: login.php

<?php 
session_start(); // 開(kāi)啟session
?> 
<?php if(isset($_SESSION['username'])): ?>
	<p>您已經(jīng)登錄了,請(qǐng)不要重復(fù)登錄</p>
	<p>正在跳轉(zhuǎn)中...</p>
	<script>setTimeout('location.href="home.php"',2000)</script>
<?php else: ?>
<html>
<head>
	<style>
		form{width:240;height:200px;margin:5% auto;}
		input{border:0;border-bottom:1px solid #616161;}
		.btn{width:60;height:20px;margin:5px 80px;}
		p{margin:20px 80px;width:240px;}
		.success{color:green;}
		.error{color:red;}
	</style>
</head>
<body>
	<form name="user">
		<p>
			<label>賬號(hào):<label>
			<input type="text" name="account" placeholder="123456">
		</p>
		
		<p>
			<label>密碼:<label>
			<input type="password" name="password" placeholder="******">
		</p>
		<div class="btn">
			<button type="button" onclick="check(this.form);return false;">登錄</button>
		</div>
		
		<p><!-- 登陸狀態(tài)提示 --></p>
	</form>
</body>
</html>
<script>
	var user = document.forms.namedItem('user'); // 獲取整個(gè)form元素包括子元素
	var tips = user.lastElementChild; // 獲取form元素最后一個(gè)子元素
	// console.log(user.tips)
	
	/*
	@param ele 事件對(duì)象
	@param tips 提示信息的顯示元素
	@param msg 提示信息
	addEventListener('事件','函數(shù)','bool值')方法用于指定元素添加事件 blur click keydown keyup 等
	注意:去掉on 如果onclick = click
	*/
	function addEvent(ele,tips,msg){
		ele.addEventListener('blur',function(){
			if(this.value.trim().length === 0){
				tips.classList.add('error');
				tips.innerHTML = msg;
				this.focus();
			}
		},false);
		
		ele.addEventListener('keydown',function(){
			tips.innerHTML = '';
		},false);
	}
	
	addEvent(user.account,tips,'請(qǐng)輸入賬號(hào)');
	addEvent(user.password,tips,'請(qǐng)輸入密碼');
	
	// 觸發(fā)ajax登錄驗(yàn)證
	function check(form){
		// console.log(form.account.value.trim());
		var request = new XMLHttpRequest();
		
		request.onreadystatechange = function(){
			if(request.readyState ===4 && request.status === 200){
				// 將返回的json_encode數(shù)據(jù)轉(zhuǎn)為對(duì)象
				var res = JSON.parse(request.responseText); 
				// console.log(res);
				if(res.status === 1){
					tips.classList.remove('error');
					tips.classList.add('success');
					tips.innerHTML = res.message;
					setTimeout(function(){
						location.href = 'home.php';
					},2000);
				}else{
					tips.classList.add('error');
					tips.innerHTML = res.message;
				}
			}
		}
		
		request.open('POST', './check_login.php', true);
		request.setRequestHeader('content-type','application/x-www-form-urlencoded');
		var data = 'account='+form.account.value.trim()+'&password='+form.password.value.trim();
		request.send(data);
	}

</script>
<?php endif; ?>

檢查提交登錄的賬號(hào)密碼: check_login.php

<?php
	// 開(kāi)啟session_start()
	session_start();

	// 接收提交過(guò)來(lái)的數(shù)據(jù)
	$account = trim($_POST['account']);
	$password = trim($_POST['password']);
	
	// 對(duì)賬號(hào)密碼判斷是否為空
	$status = 0;   // 初始化狀態(tài)
	$message = ''; // 初始化信息
	if(empty($account)){
		$messge = '賬號(hào)不能為空!';
		exit(json_encode(['status'=>$status, 'message'=>$message])); // 將json返回到Ajax
	}else{
		$account = strtolower($account); // strtolower()函數(shù)將字母轉(zhuǎn)為小寫(xiě)
	}
	if(empty($password)){
		$message = '密碼不能為空!';
		exit(json_encode(['status'=>$status, 'message'=>$message]));
	}else{
		$password = sha1($password); 	 // sha1() 對(duì)密碼進(jìn)行40位長(zhǎng)度加密
	}
	
	// 判斷前面賬號(hào)密碼是否處理完畢,ok就進(jìn)行與數(shù)據(jù)庫(kù)驗(yàn)證操作
	if($account && $password){
		$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
		$sql = "SELECT COUNT(*) FROM `user` WHERE `account`=:account AND `password`=:password";
		$stmt = $pdo->prepare($sql);		 // 準(zhǔn)備查詢(xún)
		$result = $stmt->execute(['account'=>$account, 'password'=>$password]);
		
		if($result){
			if($stmt->fetchColumn(0) > 0){
				$sql = "SELECT * FROM `user` WHERE `account`=:account AND `password`=:password";
				$stmt = $pdo->prepare($sql);
				$stmt->bindParam(':account',$account,PDO::PARAM_INT);
				$stmt->bindParam(':password',$password,PDO::PARAM_STR);
				$stmt->execute();
				$user = $stmt->fetch(PDO::FETCH_ASSOC);
				// echo '<pre>';print_r($user);
				
				// 將從數(shù)據(jù)庫(kù)獲取的用戶(hù)id和用戶(hù)名賦給$_SESSION
				$_SESSION['user_id'] = $user['id'];
				$_SESSION['username'] = $user['username'];
				
				$ststus = 1;
				$message = '登錄成功,正在跳轉(zhuǎn)...';
				exit(json_encode(['status'=>$ststus, 'message'=>$message]));
			}else{
				$message = '賬號(hào)或密碼錯(cuò)誤!';
				exit(json_encode(['status'=>$status, 'message'=>$message]));
			}
		}else{
			die(print_r($stmt->errorInfo()));
		}
	}

登錄成功跳轉(zhuǎn)到home頁(yè)面: home.php

<?php 
	require "./is_session.php";
	$title = $pdo->prepare("SELECT `sitename`,`status` FROM `system`");
	$title->execute();
	$h2 = $title->fetch(PDO::FETCH_ASSOC);
	$pdo = NULL;
?>
<!DOCTYPE html>
<html>
<head>
	<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
	<style>
		*{margin:0;padding:0;font-family:'楷體';}
		h2{margin-left:10px;color:#888;}
		a{text-decoration: none;}
		li{list-style: none;color:#888;}
		.big-box{width: 1000px;height: 600px;margin: 10px auto;}
		.menu-box{width: 150px;height: 600px;background: skyblue;float: left;}
		.menu-box li{width: 140px;height: 30px;line-height: 30px;
			margin: 5px 0px;padding-left: 10px;}
		.menu-box li:hover{background: #ff6700;cursor: pointer;color:#fff;}
		.menu-box li a{margin-left: 20px;}	
		.right-box{width: 840px;height: 600px;float: right;border-right:10px solid skyblue;}
		.right-box .user{margin-top:8px;float:left;}
		button{margin-top:8px;margin-left:10px;float:right;display:block;}
	</style>	
</head>
<body>
<div class="big-box">
	<div class="menu-box">
		<ul>
			<h2 name="welcome.php"><?=$h2['sitename']?></h2>
			<li name="account_lists.php">用戶(hù)賬號(hào)</li>
			<?php if($h2['status']==1): ?>
			<li name="user_lists.php">用戶(hù)列表</li>
			<?php endif;?>
			<li name="system.php">系統(tǒng)設(shè)置</li>
		</ul>
	</div>
	<div class="right-box">
		<div style="width:100%;height:32px;background:skyblue;">
			<?php if(isset($_SESSION['username'])): ?>
			<p class="user">用戶(hù)名:<?=$_SESSION['username']?><span>&nbsp;|&nbsp;</span>
			<a href="javascript:;" onclick="logout()">退出</a></p>
			<?php else: ?>
			<script>location.href='login.php';</script>
			<?php endif; ?>
			<button>中文</button> <button onclick="jump()">English</button>
		</div>
		<iframe src="welcome.php" name="tag" width="100%" height="532" scrolling="no" frameborder="0"></iframe>
		<div style="width:100%;height:32px;background:skyblue;"></div>
	</div>
</div>
</body>
</html>
<script type="text/javascript">
	function jump(){
		window.location.href = 'admin_en/home.php';
	}
	
	$(document).ready(function(){
		$('.menu-box li').click(function(){
			var src = $(this).attr('name');
			$('iframe').attr('src',src);
		});
		$('h2').click(function(){
			var src = $(this).attr('name');
			$('iframe').attr('src',src);
		});
	});
	
	function logout(){
		var out =  confirm('你想退出嗎?');
		if(out==true){
			location.href = 'logout.php';
		}else{
			false;
		}
	}
</script>

賬號(hào)密碼管理列表: account_lists.php

<?php
	require "./is_session.php";
	$user_acc = "SELECT * FROM `user`";
	$stmt = $pdo->prepare($user_acc);
	$stmt->execute();
	// print_r($stmt->fetch(PDO::FETCH_ASSOC));
	$pdo = NULL;
?>
<html>
<head>
	<style>
		table{margin:0 auto;width:100%;}
		tr,td,th{border:1px solid #888;text-align:center;}
		th{background:pink;}
		caption{font-size:22px;margin:10px 0;font-weight:bold;}
		.success{color:green;}
		.error{color:red;};
	</style>
</head>
<body>
	<table border="0" cellpadding="0" cellspacing="0">
		<caption>賬號(hào)管理</caption>
		<tr>
			<th>ID</th>
			<th>名稱(chēng)</th>
			<th>賬號(hào)</th>
			<th>密碼</th>
			<th>郵箱</th>
			<th>狀態(tài)</th>
			<th>添加時(shí)間</th>
			<th>操作<button style="float:right;">新增</button></th>
		</tr>
		<?php
		// 返回索引為結(jié)果集列名的數(shù)組,循環(huán)輸出到表格里
		while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
		?>
		<tr>
			<td><?=$row['id'] ?></td>
			<td><?=$row['username'] ?></td>
			<td><?=$row['account'] ?></td>
			<td><?=$row['password'] ?></td>
			<td><?=$row['email'] ?></td>
			<td><?=($row['status']==1)?'Ok':'No' ?></td>
			<td><?=$row['addtime'] ?></td>
			<td>
				<button onclick="edit(<?=$row['id']?>)">編輯</button>
				<button onclick="del(<?=$row['id']?>)">刪除</button>
			</td>
		</tr>
		<?php 
		} 
		unset($pdo);
		?>
	</table>
	
	<p id="msg"></p>
</body>
</html>
<script>
	function edit(id){
		location.href = 'account_add.php?id='+id; 
	}
	function del(id){
		if(confirm('你確定要?jiǎng)h除嗎?')){
			var request = new XMLHttpRequest();
			request.onreadystatechange = function(){
				if(request.readyState == 4 && request.status == 200){
					// 特級(jí)注意: JSON.parse() 不能同時(shí)處理2個(gè)回調(diào)
					var res = JSON.parse(request.responseText);
					var tips = document.getElementById('msg');
					tips.innerHTML = res.mssage;
					if(res.status == 1){
						tips.classList.add('success');
					}else{
						tips.classList.add('error');
					}
				}
				setTimeout(function(){
					location.reload();
				},1000);
			}
			request.open('POST','./account_manage.php?action=del');
			request.setRequestHeader('content-type','application/x-www-form-urlencoded');
			request.send('id='+id);
		}else{
			return false;
		}
	}
</script>

賬號(hào)密碼添加: account_add.php  (添加和編輯差不多所以沒(méi)有寫(xiě)添加只寫(xiě)了編輯)

<?php 
	require "./is_session.php";
	$id = (int)trim($_GET['id']);
	$sql_edit = "SELECT * FROM `user` WHERE `id`=:id";
	$stmt = $pdo->prepare($sql_edit);
	$stmt->bindParam(':id',$id,PDO::PARAM_INT);
	$stmt->execute();
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	unset($pdo);
?>
<html>
<head>
	<style>
		label{width:65px;display:block;float:left;padding-right:5px;}
		.box{width:320px;height:240px;background:#FFFAFA;margin:30px auto;}
		.success{color:green;}
		.error{color:red;}
	</style>
</head>
<body>
<div class="box">
	<form method="POST">
		<input type="hidden" name="id" value="<?=$row['id']?>">
		
		<p>
		<label>賬號(hào):</label>
		<input type="text" name="account" value="<?=$row['account']?>" disabled>
		</p>
		
		<p>
		<label>名稱(chēng):</label>
		<input type="text" name="username" value="<?=$row['username']?>">
		</p>
		
		<p>
		<label>密碼:</label>
		<input type="password" name="password" value="<?=$row['password']?>">
		</p>
		
		<p>
		<label>郵箱:</label>
		<input type="text" name="email" value="<?=$row['email']?>">
		</p>
		
		<p>
		<label>顯示:</label>
		<input type="radio" name="status" value="<?=($row['status']==1) ? $row['status'] : 1 ?>" 
		<?=($row['status']==1) ? 'checked' : '' ?> >
		</p>
		
		<p>
		<label>隱藏:</label>
		<input type="radio" name="status" value="<?=($row['status']==0) ? $row['status'] : 0 ?>"
		<?=($row['status']==0) ? 'checked' : '' ?> ><br>
		</p>
		
		<p style="margin-left:65px;">
		<button onclick="save(this.form);return false;">保存</button>
		<button onclick="history.back();return false;" style="margin-left:60px;">返回</button>
		</p>
		
		<p style="margin-left:65px;"></p>
	</form>
	
<div>	
</body>
</html>
<script>
	function save(form){
		var request = new XMLHttpRequest();
		
		request.onreadystatechange = function(){
			if(request.readyState === 4 && request.status === 200){
				var res = JSON.parse(request.responseText);
				// console.log(request.responseText)
				var tips = form.lastElementChild; // 獲取form標(biāo)簽最后一個(gè)子元素
				tips.innerHTML = res.mssage; // 給子元素添加json返回的信息
				
				if(res.status == 1){
					tips.classList.add('success');
					setTimeout(function(){
						self.location = document.referrer;
					},1000);
				}else{
					tips.classList.add('error');
				}
			}
		}
		
		request.open('POST','./account_manage.php?action=save');
		request.setRequestHeader('content-type','application/x-www-form-urlencoded');
		var data = 'id='+form.id.value+'&username='+form.username.value+'&password='+form.password.value+'&email='+form.email.value+'&status='+form.status.value;
		request.send(data);
	}
</script>

編輯賬號(hào)處理: account_manage.php

<?php
	require "./is_session.php";
	
	// 將接收的get值變?yōu)樾?xiě)和去除左右空格
	$action = strtolower(trim($_GET['action']));
	$id   = (int)$_POST['id'];
	
	switch($action){
		case 'save':
			$username = $_POST['username'];
			$password = trim($_POST['password']);
			$email  = $_POST['email'];
			$status = $_POST['status'];
			$sql = 'UPDATE `user` SET `username`=:username,`password`=:password,`email`=:email,';
			$sql .= '`status`=:status WHERE `id`=:id';
			
			$result = $pdo->prepare($sql);
			$result->bindParam(':username',$username,PDO::PARAM_STR);
			$result->bindParam(':password',$password,PDO::PARAM_STR);
			$result->bindParam(':email',$email,PDO::PARAM_STR);
			$result->bindParam(':status',$status,PDO::PARAM_INT);
			$result->bindParam(':id',$id,PDO::PARAM_INT);
			
			if($result->execute()){
				if($result->rowCount() === 1){
					$status = 1;
					$mssage = '更新成功';
				}else if($result->rowCount() ==0 ){
					$status = 0;
					$mssage = '無(wú)效更新';
				}else{
					$status = -1;
					$mssage = '更新錯(cuò)誤,請(qǐng)檢查!';
				}
			}
			echo json_encode(['status'=>$status,'mssage'=>$mssage]);
		break;
		case 'del';
			$sql = 'DELETE FROM `user` WHERE `id`=:id';
			$res = $pdo->prepare($sql);
			$res->execute(['id'=>$id]);
				
			if($res->rowCount() == 1){
				$status = 1;
				$mssage = '刪除成功';
			}else if($res->rowCount() == 0){
				$status = 0;
				$mssage = '無(wú)效刪除';
			}else{
				$status = -1;
				$mssage = '刪除錯(cuò)誤,請(qǐng)檢查!';
			}
			exit(json_encode(['status'=>$status, 'mssage'=>$mssage]));
		break;
	}
	$pdo = NULL; // 斷開(kāi)PDO連接

人員管理列表: user_lists.php

<?php
	require "./is_session.php"; // 引入sql連接文件
	
	$pageNum = 3; // 設(shè)置每頁(yè)顯示的數(shù)量
	$page = isset($_GET['p']) ? $_GET['p'] : $_GET['p'] = 1; // URL p 參數(shù)
	
	$stmt = $pdo->prepare("SELECT COUNT(*) FROM `staff`"); // 準(zhǔn)備查詢(xún)
	$stmt->execute(); // 執(zhí)行查詢(xún)
	$total = $stmt->fetchColumn(0); // 從結(jié)果集中返回單獨(dú)一列
	$pages = ceil($total / $pageNum);  // ceil()函數(shù)小數(shù)點(diǎn)向上取整
	// print $pages;
	$offset = ($page - 1) * $pageNum; // $_GET的數(shù)量減1再乘每頁(yè)總數(shù),得出結(jié)果就是LIMIT的偏移量
	$sql = "SELECT `id`,`name`,`age`,`course`,`mobile`,`intodate`,`status`";
	$sql .= " FROM `staff` LIMIT {$offset},{$pageNum}";
	$stmt = $pdo->prepare($sql);
	$stmt->execute();
	$staff = $stmt->fetchAll(PDO::FETCH_ASSOC);
	// echo '<pre>';print_r($staff);die;
	
	$info = $pdo->prepare("SELECT `company`,`tel` FROM `system`");
	$info->execute();
	$res = $info->fetch(PDO::FETCH_ASSOC);
?>
<html>
<head>
	<style>
		table{margin:0 auto;width:100%;}
		tr,td,th{border:1px solid #888;text-align:center;}
		th{background:pink;}
		caption{font-size:22px;margin:10px 0;font-weight:bold;}
		.success{color:green;}
		.error{color:red;}
		.page{width:22px;height:22px;border:0px solid #ccc;display:block;
			text-decoration:none;text-align:center;line-height:22px;float:left;
			margin:10px 5px;}
		.info{width:100%;height:50px;margin-top:320px;}
		.info span{margin:0 auto;display:block;}
	</style>
</head>
<body>
	<table border="0" cellpadding="0" cellspacing="0">
		<caption>用戶(hù)信息表</caption>
		<tr>
			<th>ID</th>
			<th>名稱(chēng)</th>
			<th>年齡</th>
			<th>課程</th>
			<th>聯(lián)系</th>
			<th>添加時(shí)間</th>
			<th>狀態(tài)</th>
			<th>操作<button style="float:right;" onclick="add();">新增</button></th>
		</tr>
		<?php
		// fetchAll 返回為二維數(shù)組,遍歷輸出到表格
		foreach($staff as $row){
		?>
		<tr>
			<td><?=$row['id']?></td>
			<td><?=$row['name'] ?></td>
			<td><?=$row['age'] ?></td>
			<td><?=$row['course'] ?></td>
			<td><?=$row['mobile'] ?></td>
			<td><?=date('Y-m-d',$row['intodate']) ?></td>
			<td><?=($row['status']==1)?'show':'hide' ?></td>
			<td>
				<button onclick="edit(<?=$row['id'];?>);">編輯</button>
				<button onclick="del(<?=$row['id'];?>)">刪除</button>
			</td>
		</tr>
		<?php 
		} 
		unset($pdo);
		?>
	</table>
	<a class="page" href="<?=$_SERVER['PHP_SELF']?>?p=1">首</a>
	<?php for($i=1;$i<=$pages;$i++): ?>
		<?php 
		if(isset($_GET['p']) && $_GET['p']==$i){
			$bg = 'style="background:lightblue;"';
		}else{
			$bg = "";
		}
		?>
		<a class="page" <?=$bg?> href="javascript:
		location.href='<?=$_SERVER['PHP_SELF']?>?p=<?=$i?>'"> <?=($i==3)?'...':$i?> </a>
	<?php endfor ?>
	<a class="page" href="<?=$_SERVER['PHP_SELF']?>?p=<?=$pages?>">尾</a>
	<p id="msg"></p>
	
	<div class="info">
		<span><?=$res['company']?></span>
		<span><?=$res['tel']?></span>
	</div>
</body>
</html>
<script>

	function edit(id){
		location.href = 'user_add.php?id='+id;
	}
	function add(){
		location.href = 'user_add.php';
	}
	function del(id){
		if(confirm('你確定要?jiǎng)h除嗎?')){
			var request = new XMLHttpRequest();
			request.onreadystatechange = function (){
				if(request.readyState === 4 && request.status === 200){
					var res = JSON.parse(request.responseText);
					var tips = document.getElementById('msg');
					tips.innerHTML = res.message;
					if(res.status == 1){
						tips.classList.add('success');
					}else{
						tips.classList.add('error');
					}
					setTimeout(function(){
						location.reload();
					},1000);
				}
			}
			request.open('POST','./user_manage.php?action=del',true);
			request.setRequestHeader('content-type','application/x-www-form-urlencoded');
			request.send('id='+id);
		}else{
			return false;
		}
	}

</script>

人員添加和編輯: user_add.php (添加和編輯同一個(gè)頁(yè)面,有id傳過(guò)來(lái)就是編輯反之添加)

<?php
	require "./is_session.php";

	if(!empty($_GET['id'])){
		$sql = "SELECT `id`,`name`,`age`,`course`,`mobile`,`status`";
		$sql .= " FROM `staff` WHERE `id`=:id";
		$stmt = $pdo->prepare($sql);
		$stmt->bindValue(':id',(int)$_GET['id'],PDO::PARAM_INT);
		$stmt->execute();
		$row = $stmt->fetch(PDO::FETCH_ASSOC);
		// echo '<pre>';print_r($row);die;
	}
?>
<html>
<head>
	<style>
		label{width:65px;display:block;float:left;padding-right:5px;}
		.box{width:320px;height:240px;background:#FFFAFA;margin:30px auto;}
		.success{color:green;}
		.error{color:red;}
		form{margin-left:30%;margin-top:30px;display:block;}
	</style>
</head>
<body>
	<form method="POST" name="user">
		<input type="hidden" name="id" value="<?=isset($row['id'])?$row['id']:NULL;?>">
		<p>
		<label>名稱(chēng):</label>
		<input type="text" name="name" value="<?=isset($row['name'])?$row['name']:NULL;?>">
		</p>
		
		<p>
		<label>年齡:</label>
		<input type="number" name="age" value="<?=isset($row['age'])?$row['age']:18;?>" min="18" max="200">
		</p>
		
		<p>
		<label>課程:</label>
		<input type="text" name="course" value="<?=isset($row['course'])?$row['course']:NULL;?>">
		</p>
		
		<p>
		<label>聯(lián)系:</label>
		<input type="text" name="mobile" value="<?=isset($row['mobile'])?$row['mobile']:NULL;?>">
		</p>
		
		<p>
		<label>顯示:</label>
		<input type="radio" name="status" value="<?=isset($row['status'])&&($row['status']==1) ? $row['status'] : 1;?>"
		<?=isset($row['status'])&&($row['status']==1) ? 'checked' : '';?> >
		</p>
		
		<p>
		<label>隱藏:</label>
		<input type="radio" name="status" value="<?=isset($row['status'])&&($row['status']==0) ? $row['status'] : 0;?>"
		<?=isset($row['status'])&&($row['status']==0) ? 'checked' : '';?> >
		</p>
		
		<p>
		<button onclick="save(this.form);return false;" style="margin-left:65px;">保存</button>
		<button onclick="history.back();return false;" style="margin-left:65px;">返回</button>
		</p>
		
		<p style="margin-left:65px;"></p>
	</form>
	
</body>
</html>
<script>
	var user = document.forms.namedItem('user');
	var tips = user.lastElementChild;
	
	function addEvent(ele,tips,msg){
		ele.addEventListener('blur',function(){
			if(this.value.trim().length === 0){
				tips.classList.add('error');
				tips.innerHTML = msg;
				this.focus();
			}
		},false);
		ele.addEventListener('keydown',function(){
			tips.innerHTML = '';
		},false);
	}
	addEvent(user.name,tips,'請(qǐng)輸入你的名字!');
	addEvent(user.course,tips,'請(qǐng)輸入你的課程!');
	addEvent(user.mobile,tips,'請(qǐng)輸入你的聯(lián)系方式!');
	
	function save(form){
		var request = new XMLHttpRequest();		
		request.onreadystatechange = function (){
			if(request.readyState === 4 && request.status === 200){
				var res = JSON.parse(request.responseText);
			
				tips.innerHTML = res.message;
				
				if(res.status == 1){
					tips.classList.add('success');
					
				}else{
					tips.classList.add('error');
				}
				
				setTimeout(function(){
					self.location = document.referrer;
				},1000);
			}
		}
		
		request.open('POST','./user_manage.php?action=edit',true);
		request.setRequestHeader('content-type','application/x-www-form-urlencoded');
		var data = 'id='+form.id.value
			+'&name='+form.name.value
			+'&age='+form.age.value
			+'&course='+form.course.value
			+'&mobile='+form.mobile.value
			+'&status='+form.status.value;
		request.send(data);
	}
</script>

添加,編輯,刪除處理: user_manage.php

<?php
	require "./is_session.php";
	
	$action = strtolower(trim($_GET['action']));
	$id = (int)$_POST['id'];
	
	switch($action){
		case 'edit':
			$name = trim($_POST['name']);
			$age = (int)$_POST['age'];
			$course = trim($_POST['course']);
			$mobile = trim($_POST['mobile']);
			$state = (int)$_POST['status'];
			if($id){
				$sql = "UPDATE `staff` SET `name`=:name,`age`=:age,`course`=:course,";
				$sql .= "`mobile`=:mobile,`status`=:state WHERE `id`=:id";
				$stmt = $pdo->prepare($sql);
				$stmt->bindParam(':name',$name,PDO::PARAM_STR);
				$stmt->bindParam(':age',$age,PDO::PARAM_INT);
				$stmt->bindParam(':course',$course,PDO::PARAM_STR);
				$stmt->bindParam(':mobile',$mobile,PDO::PARAM_STR);
				$stmt->bindParam(':state',$state,PDO::PARAM_INT);
				$stmt->bindParam(':id',$id,PDO::PARAM_INT);
				
				if($stmt->execute()){
					if($stmt->rowCount() == 1){
						$status = 1;
						$message = '更新成功';
					}else if($stmt->rowCount() == 0){
						$status = 0;
						$message = '無(wú)效更新';
					}else{
						$status = -1;
						$message = '更新錯(cuò)誤,請(qǐng)檢查!';
					}
				}
			}else{
				$in = time();
				$sql = "INSERT INTO `staff`(`name`,`age`,`course`,`mobile`,`intodate`,`status`)";
				$sql .= " VALUES(:name,:age,:course,:mobile,:intodate,:state)";
				$stmt = $pdo->prepare($sql);
				$stmt->bindValue(':name',$name,PDO::PARAM_STR);
				$stmt->bindValue(':age',$age,PDO::PARAM_INT);
				$stmt->bindValue(':course',$course,PDO::PARAM_STR);
				$stmt->bindValue(':mobile',$mobile,PDO::PARAM_STR);
				$stmt->bindValue(':intodate',$in,PDO::PARAM_STR);
				$stmt->bindValue(':state',$state,PDO::PARAM_INT);
				
				if($stmt->execute()){
					if($stmt->rowCount() > 0){
						$status = 1;
						$message = '新增成功';
					}else if($stmt->rowCount() == 0){
						$status = 0;
						$message = '無(wú)效新增';
					}else{
						$status = -1;
						$message = '新增錯(cuò)誤,請(qǐng)檢查!';
					}
				}
			}
			exit(json_encode(['status'=>$status, 'message'=>$message]));
		break;
		case 'del':
			$sql = "DELETE FROM `staff` WHERE `id`=:id";
			$stmt = $pdo->prepare($sql);
			$stmt->execute(['id'=>$id]);
			if($stmt->rowCount() > 0){
				$status = 1;
				$message = "刪除成功";
			}else if($stmt->rowCount() == 0){
				$status = 0;
				$message = "無(wú)效刪除";
			}else{
				$status = 0;
				$message = "刪除錯(cuò)誤,請(qǐng)檢查!";
			}
			exit(json_encode(['status'=>$status, 'message'=>$message]));
		break;
	}	

后臺(tái)設(shè)置: system.php

<?php
	error_reporting(0);
	require "./is_session.php";
	$stmt = $pdo->prepare("SELECT * FROM `system`");
	$stmt->execute();
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	
	$sql = "UPDATE `system` SET `sitename`=:name,`company`=:company,`tel`=:tel,`status`=:status WHERE `id`=:id";
	$result = $pdo->prepare($sql);
	$result->bindValue(':name',$_POST['sitename'],PDO::PARAM_STR);
	$result->bindValue(':company',$_POST['company'],PDO::PARAM_STR);
	$result->bindValue(':tel',$_POST['tel'],PDO::PARAM_STR);
	$result->bindValue(':status',$_POST['status'],PDO::PARAM_INT);
	$result->bindValue(':id',$_POST['id'],PDO::PARAM_INT);
	$result->execute();
?>
<html>
<head>
	<style>
		label{width:70px;display:block;float:left;padding-right:5px;}
		.box{width:320px;height:240px;background:#FFFAFA;margin:30px auto;}
		.success{color:green;}
		.error{color:red;}
		form{margin-left:30%;margin-top:30px;display:block;}
	</style>
</head>
<body>
	<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
		<input type="hidden" name="id" value="<?=$row['id']?>">
		<p>
		<label>網(wǎng)站名稱(chēng):</label>
		<input type="text" name="sitename" value="<?=$row['sitename']?>">
		</p>
		
		<p>
		<label>公司名稱(chēng):</label>
		<input type="text" name="company" value="<?=$row['company']?>">
		</p>
		
		<p>
		<label>公司電話:</label>
		<input type="text" name="tel" value="<?=$row['tel']?>">
		</p>
		
		<p>
		<label>菜單顯示:</label>
		<input type="radio" name="status" value="<?=($row['status']==1)?$row['status']:1?>"
		<?=($row['status']==1)?checked:''?>
		</p>
		
		<p>
		<label>菜單隱藏:</label>
		<input type="radio" name="status" value="<?=($row['status']==0)?$row['status']:0?>"
		<?=($row['status']==0)?checked:''?>>
		</p>
		
		<p>
		<button type="submit" style="margin-left:65px;">保存</button>
		</p>
		
		<p style="margin-left:65px;"></p>
	</form>
	
</body>
</html>

home.php登錄后默認(rèn)顯示的頁(yè)面: welcome.php

<html>
<body bgcolor="#ccc">
<?php
	$sysinfo = array(
	        '操作系統(tǒng)'     => PHP_OS,
            '運(yùn)行環(huán)境'     => $_SERVER['SERVER_SOFTWARE'],
            'PHP運(yùn)行方式'  => php_sapi_name(),
            '上傳附件限制'  => ini_get('upload_max_filesize'),
            '執(zhí)行時(shí)間限制'  => ini_get('max_execution_time').'秒',
            '服務(wù)器時(shí)間'   => date("Y年n月j日 H:i:s"),
            '北京時(shí)間'     => gmdate("Y年n月j日 H:i:s",time()+8*3600),
            '服務(wù)器域名/IP'=> $_SERVER['SERVER_NAME'].'['.gethostbyname($_SERVER['SERVER_NAME']).']',
            '剩余空間'     => round((disk_free_space(".")/(1024*1024)),2).'M',
		);
		echo '<pre>';
		print_r($sysinfo);
?>
</body>
</html>

退出登錄: logout.php

<?php
	session_start();
	
	if($_SESSION['user_id'] && $_SESSION['username']){
		session_destroy();
		setcookie('PHPSESSID','',time()-3600,'/');
		header('location:login.php');
	}


MySQL結(jié)構(gòu) : php.sql

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for staff
-- ----------------------------
DROP TABLE IF EXISTS `staff`;
CREATE TABLE `staff` (
  `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
  `pid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '父id',
  `name` char(40) NOT NULL COMMENT '員工名字',
  `age` tinyint(3) unsigned NOT NULL COMMENT '年齡',
  `course` varchar(50) DEFAULT NULL COMMENT '職位',
  `mobile` char(11) DEFAULT NULL,
  `intodate` char(50) NOT NULL COMMENT '入職時(shí)間',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '1:顯示,  0隱藏',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of staff
-- ----------------------------
INSERT INTO `staff` VALUES ('13', '0', 'Jhon', '23', 'HTML5', '14514962864', '1550915121', '1');
INSERT INTO `staff` VALUES ('14', '0', 'Jack', '23', 'PHP', '19514972867', '1550911289', '0');
INSERT INTO `staff` VALUES ('16', '0', 'Pack', '25', 'C語(yǔ)言', '15614972867', '1550911206', '1');
INSERT INTO `staff` VALUES ('17', '0', 'Jerry', '28', 'Java', '17514972867', '1550911253', '1');
INSERT INTO `staff` VALUES ('18', '0', 'Tom', '31', 'CSS3', '13632159165', '1551097759', '1');
INSERT INTO `staff` VALUES ('19', '0', 'Paul', '22', 'C語(yǔ)言', '13632159165', '1551097901', '0');
INSERT INTO `staff` VALUES ('20', '0', 'Amy', '21', 'PHP', '13632159165', '1551097981', '0');
INSERT INTO `staff` VALUES ('21', '0', 'Marr', '22', 'Java', '13665425832', '1551097981', '0');
INSERT INTO `staff` VALUES ('22', '0', 'Bill', '18', 'C#', '13665425832', '1551331796', '1');
INSERT INTO `staff` VALUES ('30', '0', 'Hony', '18', 'JavaEE', '0757-52124', '1551331796', '1');
INSERT INTO `staff` VALUES ('42', '0', 'Bird', '22', 'Python', '13912536525', '1551359668', '1');
INSERT INTO `staff` VALUES ('40', '0', 'Dog', '23', 'jQuery', '13625845624', '1551359566', '1');
INSERT INTO `staff` VALUES ('41', '0', 'Cat', '19', 'Javascript', '15836912362', '1551359592', '1');
INSERT INTO `staff` VALUES ('43', '0', 'Sheep', '30', 'C++', '13912536525', '1551359769', '0');
INSERT INTO `staff` VALUES ('44', '0', 'Monkey', '26', 'Vue', '13912536678', '1551359884', '1');

-- ----------------------------
-- Table structure for system
-- ----------------------------
DROP TABLE IF EXISTS `system`;
CREATE TABLE `system` (
  `id` int(10) NOT NULL,
  `sitename` char(20) NOT NULL COMMENT '網(wǎng)站名稱(chēng)',
  `company` char(20) NOT NULL COMMENT '公司名稱(chēng)',
  `tel` char(20) NOT NULL COMMENT '公司電話',
  `status` tinyint(1) unsigned NOT NULL COMMENT '1顯示 0隱藏',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of system
-- ----------------------------
INSERT INTO `system` VALUES ('1', '資源管理', '某某科技公司', '0757-0662', '1');

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `account` int(10) unsigned NOT NULL COMMENT '登錄賬號(hào)',
  `password` char(40) NOT NULL COMMENT '密碼',
  `username` char(10) NOT NULL COMMENT '用戶(hù)名稱(chēng)',
  `email` char(20) DEFAULT NULL COMMENT '郵箱',
  `status` tinyint(1) unsigned DEFAULT '1' COMMENT '1:正常 , 0:禁用',
  `addtime` char(50) NOT NULL COMMENT '賬號(hào)添加時(shí)間',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '147147', '601f1889667efaebb33b8c12572835da3f027f78', 'Admin', 'admin@155.com', '1', '1551335673');
INSERT INTO `user` VALUES ('2', '258258', '601f1889667efaebb33b8c12572835da3f027f78', 'Jerry', 'Jerry@133.com', '1', '1551335676');
INSERT INTO `user` VALUES ('36', '369369', '601f1889667efaebb33b8c12572835da3f027f78', 'Tom', 'Tom@142.com', '1', '1551335678');

效果圖:

QQ截圖20190306115256.jpg

批改狀態(tài):合格

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
1條評(píng)論
張軒維 2020-09-01 12:11:41
試試看可能用
1樓
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)