简体   繁体   English

PHP查询无效结果

[英]PHP Query invalid result

I apologise if the title is confusing, but when I run the page with the code below and enter an email not found in the database, on the webpage I get Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\Testing\\login.php on line 72 . 如果标题令人困惑,我深表歉意,但是当我运行带有以下代码的页面并输入数据库中未找到的电子邮件时,在网页上我得到了Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\Testing\\login.php on line 72 Instead of it saying this, I want it to give an error that the email is not registered. 与其说这句话,不如说它是未注册电子邮件的错误。

    <?php
session_start();//session starts here
if(isset($_SESSION['adminName'])||isset($_SESSION['email'])){
    header("Location: welcome.php");//redirect to login page to secure the welcome page without login access.
}
?>



<html>
<head lang="en">
    <meta charset="UTF-8">
    <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
    <title>Login</title>
</head>
<style>
    .login-panel {
        margin-top: 150px;

</style>

<body>


<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-success">
                <div class="panel-heading">
                    <h3 class="panel-title">Sign In</h3>
                </div>
                <div class="panel-body">
                    <form role="form" method="post" action="login.php">
                        <fieldset>
                            <div class="form-group"  >
                                <input class="form-control" placeholder="E-Mail" name="email" type="email" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="pass" type="password" value="">
                            </div>


                                <input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="login" >

                            <!-- Change this to a button or input when using this as a form -->
                          <!--  <a href="index.html" class="btn btn-lg btn-success btn-block">Login</a> -->
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


</body>

</html>

<?php

include("database/db_conection.php");

if(isset($_POST['login'])){
    $user_email=mysqli_real_escape_string($dbcon, $_POST['email']);
    $user_pass=mysqli_real_escape_string($dbcon, $_POST['pass']);

    $encrypted_password = password_hash($user_pass, PASSWORD_BCRYPT);


    $query = $dbcon->query("SELECT user_pass FROM users WHERE user_email='$user_email'"); 
    $passwordValue=$query->fetch_object()->user_pass;

    if (password_verify($user_pass,$passwordValue)){
        echo "Success!";
    }else{
        echo $encrypted_password;
        echo "<div class='alert alert-danger'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Error!</strong> Email or password entered was incorrect!</div>";
    }




    /*$check_user="select * from users WHERE user_email='$encrypted_email' AND user_pass='$user_pass'";

    $run=mysqli_query($dbcon,$check_user);

    if(mysqli_num_rows($run))
    {
        echo "<script>window.open('welcome.php','_self')</script>";

        $_SESSION['email']=$user_email;//here session is used and value of $user_email store in $_SESSION.

    }
    else
    {
        echo "<script>alert('Email or password is incorrect!')</script>";
    }*/
}
?>

This is the code found in my login.php file. 这是在我的login.php文件中找到的代码。 The php code within the comment isn't part of the web page, I will be removing it later. 注释中的php代码不是网页的一部分,我将在以后删除它。

<html>
<head lang="en">
    <meta charset="UTF-8">
    <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">
    <title>Login</title>
</head>
<style>
    .login-panel {
        margin-top: 150px;

</style>

<body>


<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <div class="login-panel panel panel-success">
                <div class="panel-heading">
                    <h3 class="panel-title">Sign In</h3>
                </div>
                <div class="panel-body">
                    <form role="form" method="post" action="login.php">
                        <fieldset>
                            <div class="form-group"  >
                                <input class="form-control" placeholder="E-Mail" name="email" type="email" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="pass" type="password" value="">
                            </div>


                            <input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="login" >

                            <!-- Change this to a button or input when using this as a form -->
                            <!--  <a href="index.html" class="btn btn-lg btn-success btn-block">Login</a> -->
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


</body>

</html>

<?php

include("database/db_conection.php");

if(isset($_POST['login'])){
    $user_email=mysqli_real_escape_string($dbcon, $_POST['email']);
    $user_pass=mysqli_real_escape_string($dbcon, $_POST['pass']);

    $encrypted_password = password_hash($user_pass, PASSWORD_BCRYPT);


    if ($query = $dbcon->query("SELECT user_pass FROM users WHERE user_email='$user_email'") == false) {
        echo "The email doesn't exist in the DB";
    } else {
        $passwordValue=$query->fetch_object()->user_pass;

        if (password_verify($user_pass,$passwordValue)){
            echo "Success!";
        }else{
            echo $encrypted_password;
            echo "<div class='alert alert-danger'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Error!</strong> Email or password entered was incorrect!</div>";
        }




        /*$check_user="select * from users WHERE user_email='$encrypted_email' AND user_pass='$user_pass'";

        $run=mysqli_query($dbcon,$check_user);

        if(mysqli_num_rows($run))
        {
            echo "<script>window.open('welcome.php','_self')</script>";

            $_SESSION['email']=$user_email;//here session is used and value of $user_email store in $_SESSION.

        }
        else
        {
            echo "<script>alert('Email or password is incorrect!')</script>";
        }*/
    }
}
?>

I've added an if statement that checks if the query is false, if it's false it will echo that the email doesn't exist in the database, as you wanted, but if it exist, then you're fetched the password exactly as you want. 我添加了一条if语句,该语句检查查询是否为假,如果为假,它将回显您想要的数据库中不存在该电子邮件,但是如果存在,则您将完全按照以下方式获取密码你要。

I used an if statement for the code: $query->num_rows() (credit to @MasterOdin for that) and if the number of rows equaled 0, then I echoed that you typed in an invalid email. 我在代码中使用了if语句: $query->num_rows() (为此,将其贷记为@MasterOdin),如果行数等于0,则回显您键入的无效电子邮件。

Ex. 例如

if ($query->num_rows==0){
    echo "You typed an invalid email!";
}else{
    I further verified information here...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM