//php代码 获取用户输入的用户和密码用来判断
<?php
//判断表单数据提交
if(isset($_POST['username'])&& isset($_POST['password']))
{
$username = $_POST['username'];
$password = $_POST['password'];
//链接数据库
$mysqli=new MySQLi('127.0.0.1','root','root');
//选择数据库
$mysqli->select_db('mydb');
//设置字符集
$mysqli->set_charset('utf8');
//查询语句
$stament=$mysqli->prepare("select * from users where username=? and password=?");
//$sql="select * from users where username='{$username}'and password='{$password}'";
$stament->bind_param('ss',$username,$password);
//执行查询
$stament->execute();//执行sql查询
$result=$stament->get_result();
//遍历数据集$result
$row=$result->fetch_assoc();
if($row){
$result->free();//释放结果集
$mysqli->close();//释放数据库操作对象
// 校检成功后跳转页面(home.php自己编写的主页)
header("Location:home.php");
}
else{
$error='用户或密码不正确';
$result->free();//释放结果集
$mysqli->close();//释放数据库操作对象
}
}
?>
//前段代码
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="utf-8">
<link href="css/style.css" rel='stylesheet' type='text/css' />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
</head>
<body>
<!-----start-main---->
<div class="main">
<div class="login-form">
<h1>Member Login</h1>
<div class="head">
<img src="images/user.png" alt=""/>
</div>
<form action="" method="post">
<input type="text" class="text" name="username" placeholder="USERNAME">
<input type="password" name="password" placeholder="PASSWORD">
<div class="submit">
<input type="submit" onclick="myFunction()" value="LOGIN" >
</div>
<p><a href="#">Forgot Password ?</a></p>
</form>
</div>
<!--//End-login-form-->
<!-----start-copyright---->
<div class="copy-right">
<p>Copyright © 2014.Company name All rights reserved.<a target="_blank" href="http://sc.chinaz.com/moban/">网页模板</a></p>
</div>
<!-----//end-copyright---->
</div>
<?php
//输入错误后提示用户输入错误
if(isset($error))echo("<script>alert('用户名或密码不正确')</script>")
?>
</body>
</html>
PHP+Mysql 实现登录 简单的登录操作 查询预处理
最新推荐文章于 2024-04-19 05:15:00 发布