php的登錄功能和查詢數據庫內容並在web頁面顯示

需要文件:
在這裏插入圖片描述
login.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
	<title>教務處</title>
	<link rel="stylesheet" href="login.css">
</head>
<body background="photo/大背景.jpg">
	<div class="box1">
		<div class="box2">
			<form action="logincheck.php" action="main.php" method="post" name="form1">
				<div class="box2-1">學號:<input type="text" name="id"></div>
				<br>
				<div class="box2-2">
					密碼:<input type="text" name="pw">
				</div>
				<div class="box2-3">
					<input type="submit" value="登錄" style="width: 50px; height: 30px; font-size: 16px;">
				</div>
				<div>
					<a href="region.html"><input type="button" value="註冊" style="width: 50px; height: 30px; font-size: 16px;"></a>
				</div>
			</form>
		</div>
	</div>
</body>
</html>

logincheck.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php
include 'conn.php';
$id = $_POST['id'];
$pw = $_POST['pw'];
if(!$id){
    echo "<script>alert('請輸入學號!'); history.go(-1);</script>";
    exit();
}
else if(!$pw){
    echo "<script>alert('請輸入密碼'); history.go(-1);</script>";
    exit();
}
else if (strlen($id)!=11||!is_numeric($id)){
    echo "<script>alert('學號輸入有誤'); history.go(-1);</script>";
    exit();
}
$idtest = mysqli_query($conn,"select * from person where id='$id' limit 1");
if(!mysqli_fetch_array($idtest)){
    echo "<script>alert('該ID不存在,請檢查!'); history.go(-1);</script>";
    exit();
}
$che = mysqli_query($conn,"select * from person where id='$id' and password='$pw' limit 1");
if(mysqli_fetch_array($che)){
    echo "<script>alert('登錄成功!');window.location.href='main.php'</script>";
}else{
    echo "<script>alert('密碼輸入錯誤,請重新輸入!'); history.go(-1);</script>";
}
?>
</body>
</html>

login.css

* {
	margin: 0;
	padding: 0;
}
.box2 {
	margin-left: 570px;
	margin-top: 200px;
	background-color: pink;
	width: 380px;
	height: 200px;
}
.box2-1 {
	width: 400px;
	height: 47px;
	margin-top: 20px;
	margin-left: 71px;
	float: left;
}
.box2-2 {
	width: 400px;
	height: 47px;
	margin-top: 25px;
	margin-left: 71px;
	float: left;
	margin-bottom: 10px;
}
.box2-3 {
	width: 185px;
	height: 50px;
	margin-left: 65px;
	float: left;
}

連接數據庫用的conn.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php
$conn = mysqli_connect("localhost","root","root");
if(!$conn){
    die("連接數據庫失敗:".mysqli_connect_error());
}
mysqli_select_db($conn,"test");
mysqli_query($conn,'SET NAMES UTF8');
?>
</body>
</html>

測試連接數據庫的test.php

<?php
include 'conn.php';
$sql = "select * from person where name='111'";
$result = $conn->query($sql);
if($result->num_rows>0){
    while($row = $result->fetch_assoc()){
        echo $row['id']." ".$row['time'];
    }
}
?>

查詢數據庫

這裏建立了一個表格

<table style='text-align:left;border:solid' border="1">
            <tr><td>姓名</td><td>性別</td><td>聯繫方式</td></tr>
        <?php
        include 'conn.php';
        $sql = "select * from class where id='123456789'";
        $re = mysqli_query($conn,$sql);
        $num = mysqli_num_rows($re);
        for($i=0;$i<$num;$i++){
            $row = mysqli_fetch_assoc($re);
            $name = $row['membername'];
            $gender = $row['gender'];
            $tel = $row['tel'];
            echo "<tr><td>$name</td><td>$gender</td><td>$tel</td><tr>";
        }
        ?>
        </table>

查詢結果如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章