【PHP】连接数据库验证登陆

界面

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <title>登陆</title>
	  <style>
		  .mlmax{
			 max-width: 500px;
            
			  align-content: center;
			  margin: auto;
			  margin-top:30px;
		  }
	  </style>
  </head>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

	  
<!--	  主要界面-->
	  
	  

<!--	  表单-->
	<div class="mlmax border border-dark">
	  <form class="mb g-3" method="POST" action="login.php" enctype="multipart/form-data" id="bd">
		  <div class="card-header" align="center">
			  <kbd>登陆</kbd>
			 <small> | </small>
			  注册
		  </div>
		  <div class="card-body">
  <div class="col-auto">
    <label for="usr" class="visually-hidden">账号</label>
    <input type="text"  class="form-control" id="usr" placeholder="UserName" name="username">
  </div>
		  <br>

  <div class="col-auto">
    <label for="pwd" class="visually-hidden">密码</label>
    <input type="password" class="form-control" id="pwd" placeholder="PassWord" name="password">
  </div>
		  <br>

  <div class="col-auto" align="center">
    <button type="submit" class="btn btn-primary mb-3" id="bt_dl">登陆</button>
  </div>
		  </div><!--卡片内容结束-->
		  <div class="card-footer" align="center"><small>技术支持:萌狼工作室</small></div>
</form>
	  </div> 	
	  <!--JavaScript-->
	  
	  <!--隐藏的提示框-->

<!--注册新增按钮的事件-->
<script>

 </script>
  </body>
</html>

连接数据库.php

<?php
// <!--连接数据库-->
$db=new mysqli("localhost","数据库名称","数据库密码");
// 检查连接,如果连接发生错误,则退出脚本
if($db -> connect_errno){
    exit("数据库连接失败");
}  else{
    mysql_select_db($db);
}


?>

登录验证.php


<?php
include('connect.php');//连接数据库
// require_once 'connect.php';

session_start();
//获取表单内容
 $name=$_POST['username'];
 $pwd=$_POST["password"];
 // 判断
if (empty($name)) {//判断用户名是否为空
    echo "<script>alert('请输入用户名');location='login.html'</script>";
    
}else if(empty($pwd)) {//判断用户密码是否为空
    echo "
    <script>alert('请输入密码');location='login.html'</script>
    ";
}else{
        // $sql = "select username,password from admin where username='$name' and password='$pwd'";     // SQL 语句 
        $sql = "select username,password from admin where username='mllt9920' and password='qq2686485465'";
        // 执行查询
        $result=$db->query($sql);
        echo "result".$result;
        // if(mysqli_num_rows($result)=1){
        //     setcookie ( "username", $name, time () + 3600 * 24 * 365 );  
        // echo "<script>alert('登录成功');location='index.php'</script>";
        // }else{
        //     echo "<script>alert('密码错误,请重新输入');location='login.html'</script>";
        // }
    //     $result = mysqli_query($db,$sql); // 执行上面的 SQL 语句
    // echo "name:".$name."<br>";
    // echo "pwd:".$pwd."<br>";
    // echo "sql:".$sql."<br>";
    // echo "result:".$result."<br>";
    //     $row=mysqli_num_rows($result);//获取返回的结果行数
         
//下面四行是过程测试代码

// if(!$row){//如果返回结果为0行,则不存在,即是错误
//         echo "<script>alert('密码错误,请重新输入');location='login.html'</script>";
//     }
//     else{
//          setcookie ( "username", $name, time () + 3600 * 24 * 365 );  
//         echo "<script>alert('登录成功');location='index.php'</script>";
//     };
//  }
}
 ?>
posted @ 2022-04-13 17:05  萌狼蓝天  阅读(94)  评论(0编辑  收藏  举报