PHP+Mysql 实现登录 简单的登录操作 查询预处理

//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 &copy; 2014.Company name All rights reserved.<a target="_blank" href="http://sc.chinaz.com/moban/">&#x7F51;&#x9875;&#x6A21;&#x677F;</a></p> 
					</div>
				<!-----//end-copyright---->
		</div>
<?php
        //输入错误后提示用户输入错误
	if(isset($error))echo("<script>alert('用户名或密码不正确')</script>")
	
?>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值