PHP注册用户名/密码不正确

时间:2016-01-28 03:24:15

标签: php mysql html5

我不确定这里的问题是什么。用户数据在我的MySQL数据库中,并且正确。但是,当我尝试登录时,我收到一条错误消息,指出用户/密码不正确。我正在尝试使用用户的电子邮件地址登录。另外我想在会话中添加名字和用户ID。

    <?php
session_start();
include_once 'dbconnect_new.php';

if(isset($_SESSION['user'])!="")
{
    header("Location: ../index.php");
}

if(isset($_POST['btn-login']))
{
    $s_email = mysql_real_escape_string($_POST['email']);
    $s_password = mysql_real_escape_string($_POST['password']);

    $s_email = trim($s_email);
    $s_password = trim($s_password);

    $res=mysql_query("SELECT student_id, student_password, student_firstname FROM studentdata WHERE student_email='$s_email'");
    $row=mysql_fetch_array($res);

    $count = mysql_num_rows($res); // if uname/pass correct it returns must be 1 row

    if($count == 1 && $row['student_password']==md5($s_password))
    {
        $_SESSION['user'] = $row['student_id'];
        header("Location: ../index.php");
    }
    else
    {
        ?>
    <script>
        alert('Username / Password Seems Wrong !');

    </script>
    <?php
    }

}
?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>New Reg Page</title>
            <link rel="stylesheet" href="style.css" type="text/css" />
        </head>

        <body>
            <center>
                <div id="login-form">
                    <form method="post">
                        <table align="center" width="30%" border="0">
                            <tr>
                                <td>
                                    <input type="text" name="email" placeholder="Your Email" required />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="password" name="password" placeholder="Your Password" required />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <button type="submit" name="btn-login">Sign In</button>
                                </td>
                            </tr>
                            <tr>
                                <td><a href="register_new.php">Sign Up Here</a></td>
                            </tr>
                        </table>
                    </form>
                </div>
            </center>
        </body>

        </html>

1 个答案:

答案 0 :(得分:0)

试试这段代码: -

    $s_email = mysql_real_escape_string($_POST['email']);
    $s_password = mysql_real_escape_string($_POST['password']);
    $s_email = trim($s_email);
    $s_password = md5(trim($s_password));

    $res=mysql_query("SELECT student_id, student_firstname FROM studentdata WHERE student_email='$s_email' AND student_password = '$s_password'");

    if (!$res) {
        // Debug query result by below code
        //echo 'Could not run query: ' . mysql_error();
        //exit;
        echo '<script language="javascript">';
        echo 'alert("Username / Password Seems Wrong !")';
        echo '</script>';
    }else{
      $row = mysql_fetch_row($result);
      $stu_id = $row[0];         
      $stu_fname =  $row[1];
      $_SESSION['user'] = $stu_id;
      header("Location: ../index.php");
    }  

希望这会对你有所帮助:)。

相关问题