前端页面
<form class="ui large form" method="post" action="#">
<div class="ui segment">
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="text" name="username" placeholder="用户名">
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="lock icon"></i>
<input type="password" name="password" placeholder="密码">
</div>
</div>
<button class="ui fluid large teal submit button">登 录</button>
</div>
<div class="ui error mini message"></div>
<!--<div class="ui mini negative message" >用户名和密码错误</div>-->
</form>
后端PHP代码7.3版本
<?php
header('Content-type:text/html;charset=utf-8');
//连接认证
$link = mysqli_connect('localhost:3306','root','123456') or die('连接失败');
//设定字符集
mysqli_query($link,'set names utf8');
//选择数据库
mysqli_query($link,'use myblog');
if (empty($_POST)){
require ('login.html');
}else{
$user['username'] = trim($_POST['username']);
if (empty($user['username'])){
error('用户名不能为空');
}
$user['password'] = trim($_POST['password']);
if (empty($user['password'])){
error('密码不能为空');
}
$sql ="select user_name,password from user where user_name = '$user[username]' and password = '$user[password]'"; //查询数据库中的用户名和密码 并返回集合
//$sql = "select * from user where user_name = '$user[username]'and password = '$user[password]'";
$rs = mysqli_query($link,$sql);
if (($row = mysqli_fetch_assoc($rs))>0){//取出数据判断是否>0
header('location: index.html');//链接到后台管理页面
}else{
echo '用户名或密码错误';
}
}
数据库
create table user(
id int primary key auto_increment,
user_name varchar(10) not null comment '用户名',
password varchar(50) not null comment '密码'
)charset utf8;