插入正确的登录详细信息(即用户名和密码),php和jquery中的代码后,我的代码无法登录 [英] Login failed in my code after inserting proper login details (i.e username and password), code in php and jquery

查看:88
本文介绍了插入正确的登录详细信息(即用户名和密码),php和jquery中的代码后,我的代码无法登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(php和jquery)用于学生和教师的登录名(两种访问方式都相同).在我的系统中,管理员可以创建学生和教师.创建后,详细信息将保存到数据库中.假定保存的详细信息可用于登录其管理面板.但是,问题是,当学生或教师想要使用管理员提供的登录详细信息登录时(已保存在数据库表中),它显示错误消息:登录失败,请检查您的用户名和密码. (相同的详细信息,保存到数据库表中用于登录过程).这让我头疼.如果有人可以告诉我,如果我的代码中有错误,将不胜感激.

I have following code (php and jquery) for Login for Student and Teacher (using same form for both access). In my system the admin can create Student and Teacher. Once created, the details are saved into database. The saved details is suppose to be use for login to their admin panel. But, the problem is , when Student or Teacher wants to login with the login details, provided by the admin (which has already been saved in database table), It display error message : Login Failed, Please check your username and password. (Same details, saved into database table is used for login process). This aching my head. If someone can tell me , if there is some error in my code, will be much appreciated.

login_form.php

<form id="login_form1" class="form-signin" method="post">
    <h3 class="form-signin-heading"><i class="icon-lock"></i> Sign in</h3>
    <input type="text" class="input-block-level" id="username" name="username" placeholder="Username" required>
    <input type="password" class="input-block-level" id="password" name="password" placeholder="Password" required>
    <button data-placement="right" title="Click Here to Sign In" id="signin" name="login" class="btn btn-info" type="submit"><i class="icon-signin icon-large"></i> Sign in</button>
<script type="text/javascript">
$(document).ready(function(){
    $('#signin').tooltip('show');
    $('#signin').tooltip('hide');
});
</script>
</form>
<script>
jQuery(document).ready(function(){
    jQuery("#login_form1").submit(function(e){
        e.preventDefault();
        var formData = jQuery(this).serialize();
        $.ajax({
                type: "POST",
                url: "login.php",
                data: formData,
                success: function(html){            
                    if(html=='true_teacher') {
                        $.jGrowl("Loading File Please Wait......", { sticky: true });
                        $.jGrowl("Welcome to Soch College's E- Learning Management System", { header: 'Access Granted' });
                        var delay = 1000;
                        setTimeout(function(){ window.location = 'dasboard_teacher.php'  }, delay);  
                    } else if (html == 'true'){
                        $.jGrowl("Welcome to Soch College's E- Learning Management System", { header: 'Access Granted' });
                        var delay = 1000;
                        setTimeout(function(){ window.location = 'student_notification.php'  }, delay); 
                    } else {
                        $.jGrowl("Please Check your username and Password", { header: 'Login Failed' });
                    }
                }
        });

    return false;

    });
});
</script>

login.php

<?php include('admin/dbcon.php');
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
//for student login
$query_student = mysql_query("SELECT * FROM student WHERE username='$username' AND password='$password'");
$count_stu = mysql_num_rows($query_student);
$row_stu = mysql_fetch_array($query_student);
//for teacher login
$query_teacher = mysql_query("SELECT * FROM teacher WHERE username='$username' AND password='$password'")or die(mysql_error());
$count_tea = mysql_num_rows($query_teacher);
$row_tea = mysql_fetch_array($query_teacher);
if( $count_stu > 0 ) { 
    $_SESSION['id']=$row_student['student_id'];
    echo 'true';
}else if( $count_tea > 0 ) { 
    $_SESSION['id']=$row_teacher['teacher_id'];
    echo 'true_teacher';
}
else{ 

}?>

推荐答案

我认为问题出在这里;

I think the problem is here;

if( $count_stu > 0 ) { 
    //$_SESSION['id']=$row_student['student_id'];// mistake
    $_SESSION['id']=$row_stu['student_id'];
    echo 'true';
}else if( $count_tea > 0 ) { 
    //$_SESSION['id']=$row_teacher['teacher_id'];// mistake
    $_SESSION['id']=$row_tea['teacher_id'];
    echo 'true_teacher';
}
else{ 
    echo 'Wrong Username Or Password';
}

我同意@Drew Pierce的观点,您应该考虑使用pdo或mysqli.

I agree with @Drew Pierce, you should consider using pdo or mysqli.

这篇关于插入正确的登录详细信息(即用户名和密码),php和jquery中的代码后,我的代码无法登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆