登录的准备好的语句不会从数据库中提取数据

时间:2019-05-16 12:23:28

标签: php mysql prepared-statement login-page

我正在尝试使用准备好的语句来创建登录页面。但是,单击登录按钮后,它什么也没做。我尝试使用中间打印语句,但是它没有回显从数据库中提取的值。

我是该领域的新手,并在google的帮助下尝试了所有可能的组合,但未找到任何内容。也欢迎使用任何其他替代代码和类似代码。

<html>
<head>
<title>Login</title>
</head>
<body>


        <form action="" method="POST"  autocomplete="off">  
                <b>Username:</b>
                <input type="text" class="textbox" size="15" name="user">
            <br>    
                <b>Password:</b>
                <input type="password" class="textbox" size="15" name="pass">
            <br>
            <input  type="submit" value="Login" name="submit" />  
        </form>  


    <?php  

if(isset($_POST["submit"])){   
if(!empty($_POST['user']) && !empty($_POST['pass'])) 
  {  
    $user=$_POST['user'];  
    $pass=$_POST['pass'];  


    $con=new mysqli('localhost','root','','myDB');  

    $stmt = $con->prepare("SELECT * FROM 'login' WHERE username = ? AND password = ?");
    $stmt->bind_param('ss', $user, $pass);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
    $dbusername=$row['username'];  
    $dbpassword=$row['password'];   
    if($user == $dbusername && $pass == $dbpassword )  
    {  
            session_start();  
                $_SESSION['sess_user']=$user;  
                $_SESSION['start'] = time();  
                $_SESSION['expire'] = $_SESSION['start'] + 1800;
        /* Redirect browser */  
            header("Location: homepage.php");  
    }  
    elseif($user != $dbusername || $pass != $dbpassword)
    {  
        echo '<span style="color:#f00"><b>Invalid username or password !</b></span>';  
    }   

    else{echo '<span style="color:#f00"><b>Some Error has occurred !</b></span>';}
  } 
  elseif (empty($_POST['user']) || empty($_POST['pass']))
  {  
    echo '<span style="color:#f00"><b>Username & Password cannot be empty !</b></span>';  
  }
}
$stmt->close();   
?>
</body>
</html>

0 个答案:

没有答案