PHP实验三:使用PHP+mysql实现登录

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<div class="by">
			<div class="center">
				<div class="login">
					<form action="/mysql/success.php" method="post">
						<h3>PHP登录</h3>
						<label>用户名称</label>
						<input type="text" placeholder="请输入账号" name="username" />
						<label style="margin-top: 10px;">密码</label>
						<input type="password" placeholder="请输入您的密码" name="pwd"  />
						<div style="margin-top: 50px;">
							<input type="submit" value="登录" class="btn">
							<button type="reset" class="btn">重置</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</body>
</html>

<?php
//通过 error 值,确定提示信息
if(!empty($_GET['error'])){
  $error=$_GET['error'];
  If($error==1){
    Echo "<script>
    alert('账号密码不正确!')
      </script>";
  }
}
?>
<style>
	* {
		padding: 0;
		margin: 0;
		box-sizing: border-box;
	}

	.by {
		background: url(https://img-blog.csdnimg.cn/20200427215737240.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1dyajY4MTE=,size_16,color_FFFFFF,t_70) no-repeat;
		width: 100%;
		min-height: 100vh;
		background-size: 100% 100%;
		background-attachment: fixed;
		position: relative;
		z-index: 10;
	}

	.center {
		min-height: 100vh;
		width: 1250px;
		margin: 0 auto;
	}

	.login {
		width: 450px;
		height: 380px;
		border: 1px solid red;
		margin: 0 auto;
		background-color: rgba(0, 0, 0, .13);
		position: absolute;
		transform: translate(-50%, -50%);
		top: 50%;
		left: 50%;
		border-radius: 10px;
		-webkit-backdrop-filter: blur(10px);
		backdrop-filter: blur(10px);
		border: 2px solid hsla(0, 0%, 100%, .09);
		box-shadow: 0 0 40px rgba(20, 18, 42, .6);
		padding: 50px 35px;
	}

	form {
		font-family: Poppins, sans-serif;
		color: #fff;
		letter-spacing: .5px;
		outline: none;
		border: none;
	}

	form h3 {
		font-size: 30px;
		text-align: center;
		color: aliceblue;
	}

	form label {
		font-size: 15px;
		color: aliceblue;
	}

	input {
		display: block;
		height: 40px;
		width: 100%;
		background-color: hsla(0, 0%, 100%, .07);
		border-radius: 3px;
		padding: 0 10px;
		margin-top: 8px;
		font-size: 14px;
		font-weight: 300;
		border: 0;
		color: #fff;
	}

	input:focus {
		outline: none;
		/* 移除默认的蓝色边框 */
	}

	.btn {
		background-color: #fff;
		color: black;
		height: 40px;
		width: 45%;
		border-radius: 3px;
		float: left;
		padding: 0 10px;
		margin-top: 8px;
		font-size: 14px;
		font-weight: 300;
		border: 0;
		margin: 0 5px;
		cursor: pointer;
	}
</style>

使用mysqli

<?php
$servername="localhost";
$usernme="root";
$pwd="123456";
$dname="php";
$port="3306";
// 创建连接
$conn = new mysqli($servername, $usernme, $pwd, $dname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: ". $conn->connect_error);
}
// 语法mysqli_connect([$host, $username, $passwd, $dname, $port, $socket] )
// $conn = mysqli_connect($servername,$usernme,$pwd,$dname, $port);
// // 检查连接
// if(!$conn){
//   die("连接失败" . mysqli_connect_error());
// }
// 关闭资源
// mysqli_close($conn);
?>

跳转成功php

<?php 
include "conn.php";
$username=$_POST["username"];
$pwd=$_POST["pwd"];
$sql="SELECT * FROM tb_user where user_name='".$username."' and pwd='".$pwd."'";
$result = $conn->query($sql);
echo $sql."<br />";
$num_rows = mysqli_num_rows($result);
if($num_rows>0){
  echo "登录成功!";
}
else{
  Header("Location:/mysql/login.php?error=1");
}
?>


mysql5

/*
Navicat MySQL Data Transfer

Source Server         : root
Source Server Version : 50726
Source Host           : localhost:3306
Source Database       : php

Target Server Type    : MYSQL
Target Server Version : 50726
File Encoding         : 65001

Date: 2024-03-22 12:52:43
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` int(11) NOT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `pwd` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'admin', '123456');

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值