为什么我这个登录页面。当打开那个页面就显示用户不存在,请重新输入呢?

来源:1-2 用户注册表单验证

hello_phpinfo

2018-10-20 20:48:02

<?php

  include_once './lib/func.php';

if(!empty($_POST['username'])){

  $username=trim($_POST['username']);

  $password=trim($_POST['password']);

  if(!$username){

    echo '用户名不能为空';

  }

  if(!$password){

    echo '密码不能为空';

  }

}

//数据库操作

$con=mysqlInit('127.0.0.1','root','','imooc_mall');

if(!$con){

  echo mysql_errno();

  exit;

}

//判断用户是否存在


@$sql="SELECT * FROM im_user WHERE username='{$username}' LIMIT 1";

$obj=mysql_query($sql);

$result=mysql_fetch_assoc($obj);


 if(is_array($result)&&!empty($result)){

    if(createPassword($password)===$result['password']){

      echo '登录成功';

      exit;

    }else{

      echo '密码输入错误,请重新输入';

      exit;

    }

 }else{

   echo '用户不存在,请重新输入';

 }

?>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>M-GALLARY|用户登录</title>

    <link type="text/css" rel="stylesheet" href="./static/css/common.css">

    <link type="text/css" rel="stylesheet" href="./static/css/add.css">

    <link rel="stylesheet" type="text/css" href="./static/css/login.css">

</head>

<body>

<div class="header">

    <div class="logo f1">

        <img src="./static/image/logo.png">

    </div>

    <div class="auth fr">

        <ul>

            <li><a href="login.php">登录</a></li>

            <li><a href="register.php">注册</a></li>

        </ul>

    </div>

</div>

<div class="content">

    <div class="center">

        <div class="center-login">

            <div class="login-banner">

                <a href="#"><img src="./static/image/login_banner.png" alt=""></a>

            </div>

            <div class="user-login">

                <div class="user-box">

                    <div class="user-title">

                        <p>用户登录</p>

                    </div>

                    <form class="login-table" name="login" id="login-form" action="login.php" method="post">

                        <div class="login-left">

                            <label class="username">用户名</label>

                            <input type="text" class="yhmiput" name="username" placeholder="Username" id="username">

                        </div>

                        <div class="login-right">

                            <label class="passwd">密码</label>

                            <input type="password" class="yhmiput" name="password" placeholder="Password" id="password">

                        </div>

                        <div class="login-btn">

                            <button type="submit">登录</button>

                        </div>

                    </form>


                </div>

            </div>

        </div>

    </div>

</div>

<div class="footer">

    <p><span>M-GALLARY</span> ©2017 POWERED BY IMOOC.INC</p>

</div>


</body>

<script src="./static/js/jquery-1.10.2.min.js"></script>

<script src="./static/js/layer/layer.js"></script>

<script>

    $(function () {

        $('#login-form').submit(function () {

            var username = $('#username').val(),

                password = $('#password').val();

            if (username == '' || username.length <= 0) {

                layer.tips('用户名不能为空', '#username', {time: 2000, tips: 2});

                $('#username').focus();

                return false;

            }


            if (password == '' || password.length <= 0) {

                layer.tips('密码不能为空', '#password', {time: 2000, tips: 2});

                $('#password').focus();

                return false;

            }



            return true;

        })


    })

</script>

</html>

http://img.mukewang.com/climg/5bcb239d000121db18060800.jpg

写回答

1回答

好帮手慕查理

2018-10-21

您好,应该是你if嵌套的问题,按照顺序执行当执行到 if(is_array($result)&&!empty($result))判断时,用户不存在所以输出了提示语句。应该是先判断是登录,当登录时才执行此if判断语句。祝学习愉快!

0

0 学习 · 2569 问题

查看课程