1. 程式人生 > >php簡單登入註冊驗證

php簡單登入註冊驗證

列表頁

<?php
//連線資料庫
$db = new MySQLi('localhost','root','','z_1032');
!mysqli_connect_error() or die('連線失敗');
$db->query('set names utf8');

//判斷修改狀態
if(!empty($_GET['id'])){
	$sno = $_GET['id'];
	$sql = "update student set status = 1 where sno = '$sno'";
	$res = $db->query($sql);
}


//查資料
$sql = "select * from student where status >= 0";
$res = $db->query($sql);
$arr = $res->fetch_all();




?>




<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>列表頁</title>
<style>
	.spanErr{
		background: red;
		cursor: pointer;
	}
	.spanSu{
		background: green;
		cursor: pointer;
	}
	
</style>
</head>

<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
	<tr>
		<th>學號</th>
		<th>姓名</th>
		<th>性別</th>
		<th>生日</th>
		<th>班級</th>
		<th>操作</th>
	</tr>
<!--	迴圈新增資料-->
	<?php foreach($arr as $v) { ?>
		<tr>
			<td><?php echo $v[0]; ?></td>
			<td><?php echo $v[1]; ?></td>
			<td><?php echo $v[2]; ?></td>
			<td><?php echo $v[3]; ?></td>
			<td><?php echo $v[4]; ?></td>
			<td><?php echo $v[5]==0 ? "<a href='list.php?id=$v[0]'><span class='spanErr'>未稽核</span></a>" : '<span class="spanSu">已通過</span>'; ?></td>
			<!-- a標籤傳值,把sno傳過去,判斷哪個的狀態修改 -->
		</tr>
	<?php } ?>
	
	
</table>
</body>
</html>

  登入頁

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>登入頁</title>
</head>

<body>
<form action="php.php" method="post">
	賬號: <input type="text" name="sname"><br>
	密碼: <input type="text" name="sno"><br>
	<button>登入</button>
	<a href="zhuce.php">註冊</a>
</form>
</body>
</html>

  註冊頁

<?php
if(!empty($sno)){
	//連線資料庫
	$db = new MySQLi('localhost','root','','z_1032');
	!mysqli_connect_error() or die('連線失敗');
	$db->query('set names utf8');
	
	
	//接收值
	$sno = $_POST['sno'];
	$sname = $_POST['sname'];
	$ssex = $_POST['ssex'];
	$sbirthday = $_POST['sbirthday'];
	$class = $_POST['class'];
	
	
	//執行sql
	$sql = "insert into student values ('$sno','$sname','$ssex','$sbirthday','$class',0)";
	$res = $db->query($sql);
	if($res){
		echo "註冊成功";
	}else{
		echo "註冊失敗";
	}
	
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>註冊頁</title>
</head>

<body>
<form action="#" method="post">
	學號: <input type="text" name="sno"><br>
	姓名: <input type="text" name="sname"><br>
	性別: <input type="text" name="ssex"><br>
	生日: <input type="text" name="sbirthday"><br>
	班級: <input type="text" name="class"><br>
	<button>註冊</button>
	<a href="list.php">檢視使用者列表</a>
</form>
</body>
</html>

  後臺頁

<?php
//連線資料庫
$db = new MySQLi('localhost','root','','z_1032');
!mysqli_connect_error() or die('連線失敗');
$db->query('set names utf8');

//接收資料
$sname = $_POST['sname'];//賬號
$sno = $_POST['sno'];//密碼


//查詢 稽核狀態為通過或者是管理員
$sql = "select sno from student where sname = $sname and status != 0";
$res = $db->query($sql);
$arr = $res->fetch_row();  //結果集專陣列, 結果是一維陣列

//判斷給出結果
if($sno != '' && $arr == ''){
	echo "賬號密碼錯誤或者未通過稽核";
}else{
	echo "登入成功";
}
?>