尝试完成一个简单的登录表单,将用户重定向到index.php,其中只允许成员。当我使用mysql时它工作正常,但当我将代码切换到mysqli时,似乎我无法使该功能起作用。
点击登录后会发生什么:输入字段重置。
我做了什么来解决这个问题:
我已将我的代码发送给朋友,当他尝试登录功能时,它对他来说很好。 他也找不到代码中的任何错误。 我已经设置了“报告所有错误”,我已经重新创建了我的数据库,创建了具有完全权限的新用户,经过我的代码几次而没有成功说出错误。希望比我更训练的人可以给我一个很好的理由,为什么我的代码不起作用。
数据库结构: phpMyAdmin的
服务器版本:5.6.20
数据库名称:websecurity 表:2
登录:用户名,密码,电子邮件,fname,lname
留言簿:用户名,评论
登录功能的代码,login.php:
<?php
session_start();
error_reporting(E_ALL); ini_set('display_errors', '1');
?>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="wrapper">
<p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>
<h4>Login</h4>
<?php
if(isset($_POST["submit"])){
//Connect
$mysqli = new mysqli("localhost", "member", "samsung", "websecurity");
if($mysqli->connect_errno) {
die("Connect failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
//Submitt pushed
if(!empty($_POST['user']) && !empty($_POST['pass'])) {
$username= strip_tags($_POST['user']);
$password= strip_tags($_POST['pass']);
$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);
// Prepare
$sql = "SELECT username,password FROM login WHERE username = ? AND password = ?";
$stmt = $mysqli->prepare($sql);
if(!$stmt) {
die("Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error);
}
//BIND
$bind_result = $stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 1){
header("location: index.php");
}
else {
echo "Wrong name or password. I won't tell you!!";
}
$stmt->free_result();
$stmt->close();
}
}
?>
<form action="" method="POST" class="form-container">
<div class="form-title"><h2>Log In</h2></div>
<div class="form-title">Username</div>
<input class="form-field" type="text" name="user" /><br />
<div class="form-title">Password</div>
<input class="form-field" type="password" name="pass" /><br />
<div class="submit-container">
<input class="submit-button" type="submit" value="Login" name="submit" />
</div>
</form>
<p>Welcome to this top secret, splendid, intelligent and very fabulous website.
If you would like to use our services we kindly ask you to register as a member first.</p>
</div>
</body>
</html>
我不知道是否有任何帮助,但这里是重定向目的地的代码,index.php:
<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:login.php");
} else {
?>
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<title>Secret Society</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link href='http://fonts.googleapis.com/css?family=Raleway:400,600,500,300,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/templatemo_main.css">
</head>
<body>
<div id="main-wrapper">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-center templatemo-logo margin-top-20">
<h2>Welcome, <?=$_SESSION['sess_user'];?>!</h2> <h4>Glad that you made it in to our secret society. Feel free to check out our
products and stay around for as long as you wish.</h4>
</div>
<div class="image-section">
<div class="image-container">
<img src="images/zoom-bg-1.jpg" id="menu-img" class="main-img inactive" alt="">
<img src="images/zoom-bg-2.jpg" id="products-img" class="inactive" alt="Product">
<img src="images/zoom-bg-3.jpg" id="services-img" class="inactive" alt="Services">
<img src="images/zoom-bg-4.jpg" id="about-img" class="inactive" alt="About">
<img src="images/zoom-bg-5.jpg" id="contact-img" class="inactive" alt="Contact">
</div>
</div>
<div class="container">
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8 col-md-offset-2 col-lg-offset-2 templatemo-content-wrapper">
<div class="templatemo-content">
<section id="menu-section" class="active">
<div class="row">
<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3 margin-bottom-20">
<a href="#products" class="change-section">
<div class="black-bg btn-menu">
<i class="fa fa-cubes"></i>
<h2>Products</h2>
</div>
</a>
</div>
<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3 margin-bottom-20">
<a href="#services" class="change-section">
<div class="black-bg btn-menu">
<i class="fa fa-laptop"></i>
<h2>Messages</h2>
</div>
</a>
</div>
<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3 margin-bottom-20">
<a href="#about" class="change-section">
<div class="black-bg btn-menu">
<i class="fa fa-users"></i>
<h2>About</h2>
</div>
</a>
</div>
<div class="col-xs-6 col-sm-3 col-md-3 col-lg-3 margin-bottom-20">
<a class="change-section">
<div class="black-bg btn-menu">
<i class="fa fa-envelope"></i>
<h2><a href="logout.php">Logout, don't leave us!</a></h2>
</div>
</a>
</div>
</div>
</section><!-- /.menu-section -->
<section id="products-section" class="inactive">
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-6 margin-bottom-20">
<div class="black-bg col-sm-12 col-md-12 col-lg-12">
<h2>Something</h2>
<p>Some texts here, and some texts over there!</p>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="black-bg col-sm-12 col-md-12 col-lg-12">
<h2>Over there!</h2>
<p> What was that?! I don't know.......</p>
</div>
</div>
</div>
<div class="row margin-top-20">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 pull-right">
<a href="#menu" class="change-section">
<div class="black-bg btn-menu">
<h2>Back to menu</h2>
</div>
</a>
</div>
</div>
</section><!-- /.product-section -->
<section id="services-section" class="inactive">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="col-sm-12 col-md-12 col-lg-12 black-bg">
<h2>Leave a message darling!</h2>
<!--Leave a message-->
<form action='guest_process.php' method='post'>
Name: <input style="color:black;" type="text" name='username' value="<?=$_SESSION['sess_user'];?>" readonly>
<p>Message: </p>
<p><textarea style="color:black;" name='comment'></textarea></p>
<hr>
<p><input style="color:black;" type='submit' name='submit' value='Post'></p>
</form>
</div>
</div>
</div>
<div class="row margin-top-20">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 pull-right">
<a href="#menu" class="change-section">
<div class="black-bg btn-menu">
<h2>Back to menu</h2>
</div>
</a>
</div>
</div>
</section><!-- /.services-section -->
<section id="about-section" class="inactive">
<div class="row">
<div class="black-bg col-sm-12 col-md-12 col-lg-12">
<h2 class="text-center">About Us</h2>
<div class="col-sm-6 col-md-6">
<p>Don't speak about us. Don't think about us. Don't write about us. It's a secret!</p>
</div>
<div class="col-sm-6 col-md-6">
<p></p>
</div>
</div>
</div>
<div class="row margin-top-20">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 pull-right">
<a href="#menu" class="change-section">
<div class="black-bg btn-menu">
<h2>Back to menu</h2>
</div>
</a>
</div>
</div>
</section><!-- /.about-section -->
<section id="contact-section" class="inactive">
<div class="row">
<div class="black-bg col-sm-12 col-md-12 col-lg-12">
<h2 class="text-center">Contact Us</h2>
<div class="col-sm-12 col-md-12">
<p>Donec at felis nec orci dapibus consectetur. <a href="#">Integer</a> hendrerit aliquet velit, bibendum accumsan mi. Integer volutpat in velit at tincidunt. Proin varius magna nec risus accumsan blandit. Morbi eget vestibulum nisi, vitae luctus elit. In in nulla a elit rutrum pellentesque.</p>
</div>
<div class="col-sm-6 col-md-6">
<div id="map-canvas"></div>
<p>456 Thamine Street, Digital Estate, Yangon 10630, Myanmar</p>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="row margin-top-20">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 pull-right">
<a href="#menu" class="change-section">
<div class="black-bg btn-menu">
<h2>Back to menu</h2>
</div>
</a>
</div>
</div>
</section>
</div>
</div>
</div>
</div>
</div><!-- /#main-wrapper -->
<div id="preloader">
<div id="status"> </div>
</div><!-- /#preloader -->
<script src="js/jquery.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/jquery.backstretch.min.js"></script>
<script src="js/templatemo_script.js"></script>
</body>
</html>
<?php
}
?>
答案 0 :(得分:0)
使用绑定尝试
$stmt->bindParam(1, $username);
$stmt->bindParam(2, $password);
$stmt -> execute();
$stmt->store_result();
而不是
$bind_result = $stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->store_result();