dsaff82024 2016-05-06 01:03
浏览 44
已采纳

PHP登录代码不起作用,用户仍然卡在登录页面上

I am new to PHP and can't find answers as to why the following code doesn't work. This should be easy, but I can't figure this out. This code produces no errors, and the SQL statement is correct in the phpAdmin SQL console. I've searched web & StackOverflow, but can't find a good answer. What's wrong? ALL users (whether in the db or not) get ignored and stuck on login page.

<?php
session_start();

//create function to check login form for admin or other type of user.      
//Redirect the admin user to the welcome page.

function login()
    {
        //strip login and password using in-build htmlspecialchars function
        $value1 = htmlspecialchars($_POST['login']);
        $value2 = htmlspecialchars($_POST['password']);    

        //set variables for the db connection
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "mydb";
        $loggedin = '';

        //Create new connection to db            
        $conn = new mysqli($servername, $username, $password, $dbname);

        //Check connection and handle any error
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            header('Locatin: login.php');
        }
        else {               
            //check if super admin user exists in db      
            $sql = "SELECT count(*) FROM admins WHERE AdminLevel = 1";
            $result = mysqli_query($conn,$sql);

            //check to see if query returns any rows
            if(mysql_num_rows(($result) > 0) {
                include 'welcome.php';
            }

            //check if the password and username match
            if(($username === $value1) && ($password === $value2)) {
                $_SESSION['loggedin'] = TRUE;
                echo "Hello ".$value1.", you are logged in!<br>";
            }
            //send user error message if login/username and password wrong
            else {
                echo "Incorrect username or password<br>";
                include 'login.php';
            }

            //close the db connection               
            $conn->close();
        }
?>

Login Form:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admin Login</title>
<script>

//function to check the form
function chkForm()
    {
        //determine the number of elements in the user login form
        var intFormLen = document.forms[0].elements.length;

        //loop through the form fields to see that a value has been input
        for (var i = 0; i < intFormLen; i++) {
            if (document.forms[0].elements[i].value == "") {
                //send user an error message if login field empty
                document.getElementById(document.forms[0].elements[i].name).innerHTML="Required Field";
                document.forms[0].elements[i].focus();                                
                return false;
            }
        }

        //clear the form fields
        function clearWarn(fieldName)
            {
                document.getElementById(fieldName).innerHTML = "";                  
                return true;
            }

        return;
    }   
</script>
</head>
<body>
<h2>Admin Login</h2>           
<div class="phpEcho">
    <div class="formLayout">
        <form action="#" method="post" onsubmit="return chkForm();">
            <label for="login">Login:</label>
            <input type="text"name="login" onchange="return clearWarn('fieldName')">
            <div id="login" style="color:red"></div><br>
            <label for="password">Password:</label>
            <input type="password" name="password" onchange="return clearWarn('fieldName')">
            <div id="password" style="color:red"></div><br><br>
            <input type="submit" name="cmdSubmit" value="Log in">
        </form>
    </div>
</div>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dqwh1203 2016-05-06 04:54
    关注

    You set your form action="#" and don't submit it in JavaScript.

    As noted by Jason, chkForm() will never return true, which would also prevent the form from submitting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路