PHP登录表单不适用于mysql

PHP登录表单不适用于mysql,php,mysql,forms,login,Php,Mysql,Forms,Login,试试这可能会对你有所帮助 <?php if(array_key_exists("logIn",$_POST)) { $link = mysqli_connect("dbaddress", "dbname", "dbpassword", "dbuser"); if(!$_POST['regno']) { $error .= "Please enter your registration number"; } if(!$_POST['

试试这可能会对你有所帮助

<?php
if(array_key_exists("logIn",$_POST))
{
     $link = mysqli_connect("dbaddress", "dbname", "dbpassword", "dbuser");

    if(!$_POST['regno'])
    {
        $error .= "Please enter your registration number";
    }
    if(!$_POST['password'])
    {
        $error .= "Password is required!";
    }
    if($error!="")
    {
        echo "<p>There were errors in your forms!</p>".$error;
    }
    else
    {
                           $query = "SELECT * FROM `users` WHERE RegistrationNo = '".mysqli_real_escape_string($link, $_POST['regno'])."'";

                $result = mysqli_query($link, $query);

                $row = mysqli_fetch_array($result);

                if (isset($row)) {

                    $hashedPassword = md5(md5($row['id']).$_POST['password']);

                    if ($hashedPassword == $row['password']) {

                        $_SESSION['id'] = $row['id'];
                        header("Location: after_login.php");
                        } 
                    else {
           $error = "That email/password combination could not be found.";
                          }                   
                          } 
                    else {
            $error = "That email/password combination could not be found.";
                 }   
                }}
?>

    <form method="post">
<center><input type="text" placeholder="Enter Username" name="regno"      id="log_username" class="sidelog"/>
<input type="password"   placeholder="Enter Password" name="password"    id="real_pass" class="sidelog"/>
</br><button id="button_log" type="submit" name="logIn" > GO </button>    </center>
</form>

试试这个可能会对你有帮助

<?php
if(array_key_exists("logIn",$_POST))
{
     $link = mysqli_connect("dbaddress", "dbname", "dbpassword", "dbuser");

    if(!$_POST['regno'])
    {
        $error .= "Please enter your registration number";
    }
    if(!$_POST['password'])
    {
        $error .= "Password is required!";
    }
    if($error!="")
    {
        echo "<p>There were errors in your forms!</p>".$error;
    }
    else
    {
                           $query = "SELECT * FROM `users` WHERE RegistrationNo = '".mysqli_real_escape_string($link, $_POST['regno'])."'";

                $result = mysqli_query($link, $query);

                $row = mysqli_fetch_array($result);

                if (isset($row)) {

                    $hashedPassword = md5(md5($row['id']).$_POST['password']);

                    if ($hashedPassword == $row['password']) {

                        $_SESSION['id'] = $row['id'];
                        header("Location: after_login.php");
                        } 
                    else {
           $error = "That email/password combination could not be found.";
                          }                   
                          } 
                    else {
            $error = "That email/password combination could not be found.";
                 }   
                }}
?>

    <form method="post">
<center><input type="text" placeholder="Enter Username" name="regno"      id="log_username" class="sidelog"/>
<input type="password"   placeholder="Enter Password" name="password"    id="real_pass" class="sidelog"/>
</br><button id="button_log" type="submit" name="logIn" > GO </button>    </center>
</form>

开始时没有
session\u start()
,所以在登录.php后会从
重定向到
?您不应该使用md5散列密码,请参阅实际代码中的@jeroen。问题中没有包括这一点。顺便说一下,当登录失败时,您没有对
$error
变量执行任何操作。添加错误处理并显示php的错误。仅供参考:md5不是一种安全的哈希方法。改为使用
password\u hash
password\u verify
。开始时没有
session\u start()
,因此在登录后会从
重定向。php
?您不应该使用md5散列密码,请参阅实际代码中的@jeroen。问题中没有包括这一点。顺便说一下,当登录失败时,您没有对
$error
变量执行任何操作。添加错误处理并显示php的错误。仅供参考:md5不是一种安全的哈希方法。使用
password\u hash
password\u verify
代替;在header()之后,它肯定会工作;在header()之后,它肯定会工作。