PHP数据库搜索验证

时间:2017-07-19 20:56:27

标签: php mysql

我使用带有SQL数据库的PHP创建了一个登录页面,我试图根据用户输入的内容输出不同的错误。例如,如果用户输入正确的“ID”而不是正确的密码,则会告诉他们密码不正确,但如果他们没有在其中输入正确的“ID”,则应告诉他们“ID /密码”不存在。目前我有他们两个工作,但我的问题是,当你第一次打开页面时,它显示错误信息“ID /密码”是错误的,我认为这与未输入任何东西有关。当输入内容时,它会正确地执行错误/正确的消息。

以下是验证发生的部分:

    $sql = "SELECT * FROM users WHERE id = '".$login."'";
    $results = pg_query($conn, $sql);

    // A CORRECT ID WAS ENTERED
    if(pg_num_rows($results)) { 

        echo("<h2 align=\"center\">Incorrect Password was Entered</h2>"); 

    // A CORRECT ID WAS NOT ENTERED
    } else {

        echo("<h2 align=\"center\">Login/password not found in the database</h2>");

    }

以下是完整的代码:

 <?php 
    include "header.php";
    ?>

    <?php
    $id = "";
    $pass = "";

    $login = isset($_POST['id']) ? $_POST['id'] : '';
    $login = !empty($_POST['id']) ? $_POST['id'] : '';
    $password = isset($_POST['pass']) ? $_POST['pass'] : '';
    $password = !empty($_POST['pass']) ? $_POST['pass'] : '';

    $sql = "SELECT first_name, last_name, email_address, last_access 
            FROM users 
            WHERE id = '".$login."' AND password= '".$password."'";
    $results = pg_query($conn, $sql);

    if(pg_num_rows($results)) { 
    $sql = "UPDATE users SET last_access = '" .date("Y-m-d",time()). "' WHERE id = '" .$login. "'";    


    $first = pg_fetch_result($results, 0);
    $last = pg_fetch_result($results, 1);
    $email = pg_fetch_result($results, 2);
    $last_access = pg_fetch_result($results, 3);

    echo("<h2 align=\"center\">Welcome back $first $last</h2>");
    echo("<h2 align=\"center\">Our records show that your</h2>");
    echo("<h2 align=\"center\">email address is $email</h2>");
    echo("<h2 align=\"center\">and you last accessed our system: $last_access</h2>");
    echo("<br/>");

    } else {

        // WHERE THE VALIDATION HAPPENS
        $sql = "SELECT * FROM users WHERE id = '".$login."'";
        $results = pg_query($conn, $sql);

        // A CORRECT ID WAS ENTERED
        if(pg_num_rows($results)) { 

            echo("<h2 align=\"center\">Incorrect Password was Entered</h2>"); 

        // A CORRECT ID WAS NOT ENTERED
        } else {

            echo("<h2 align=\"center\">Login/password not found in the database</h2>");

        }

    }

    ?>

    <h2 align="center">Please log in</h2>
<p align="center" style="margin-top:-3px">Enter your login ID and password to connect to this system</p>
<form style="margin-top:-8px" name="Input" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table border="0" bgcolor="red" cellpadding="10" style="margin-left:auto; margin-right:auto;">
<tr>
    <td style="border:hidden"><strong>Login ID</strong></td>
    <td style="border:hidden"><input type="text" name="id" value="<?php if(pg_num_rows($results)) { echo $_POST['id'];} ?>" size="20"></td>
</tr>
<tr>
    <td style="border:hidden"><strong>Password</strong></td>
    <td style="border:hidden"><input type="password" name="pass" value="" size="20"></td>
</tr>
</table>
<br/>
<table border="0" cellspacing="15" style="margin-left:auto; margin-right:auto;">
<tr>
    <td style="border:hidden"><input type="submit" value="Log In"></td>
    <td style="border:hidden"><input type="reset" value="Reset"></td>
</tr>
</table>
</form>

    <?php include "footer.php" ?>

0 个答案:

没有答案