jquery用户名验证保持失败

时间:2016-02-07 01:04:21

标签: php jquery mysqli

我正在尝试获取我的表单以检查mysql数据库中是否存在用户名。看起来javascript正在运行。我添加了用户名" tester"在我的数据库中,但当我尝试添加该名称时,我仍然收到一条消息,提示用户名可用。我的数据库连接在我的其他页面上正常工作

有人能看出出了什么问题吗?

dbconfic.inc.php

<?php
    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "root";
    $db_name  = "test";
    // connection:
    $mysqli = new mysqli($db_host, $db_user, $db_pass , $db_name);
    // tjek conenction:
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
    }

    // vi kører utf-8 på connection:
    $mysqli->set_charset("utf-8");  
?>

的index.php

<script type="text/javascript">
$(document).ready(function(){
    $('#username').keyup(function() {
    var usercheck = $(this).val();
            $('#usercheck').html('<img src="loading.gif" width="150" />');
            $.post("check.php", {user_name: usercheck} , function(data)
            {
            if (data.status == true)
            {
            $('#usercheck').parent('div').removeClass('has-error').addClass('has-success');

            } else {
            $('#usercheck').parent('div').removeClass('has-success').addClass('has-error');
            }
            $('#usercheck').html(data.msg);
            },'json');
    });
});
</script>

</head>

<body>

<form>
  <div class="form-group">
    <label for="exampleInputEmail1">User Name</label>
    <input type="text" class="form-control" name="username" id="username" placeholder="user Name">
    <span id="usercheck" class="help-block"></span>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

检查:

<?php
include 'dbconfic.inc.php';

if(isset($_POST['user_name']) && $_POST['user_name'] != '')
    {
        $response = array();
        $username = mysqli_real_escape_string($mysqli,$_POST['user_name']);
        $sql  = "SELECT username FROM login WHERE users.username='".$username."'";
        $res    = mysqli_query($mysqli, $sql);
        $count  = mysqli_num_rows($res);
        if($count > 0)
        {
            $response['status'] = false;
            $response['msg'] = 'Username already exists.';
        }
        else if(strlen($username) < 6 || strlen($username) > 15){
            $response['status'] = false;
            $response['msg'] = 'Username must be 6 to 15 characters';
        }
        else if (!preg_match("/^[a-zA-Z1-9]+$/", $username))
        {
            $response['status'] = false;
            $response['msg'] = 'Use alphanumeric characters only.';
        }
        else
        {
            $response['status'] = true;
            $response['msg'] = 'Username is available.';
        }
         echo json_encode($response);
    }?>

1 个答案:

答案 0 :(得分:1)

$sql  = "SELECT username FROM login WHERE users.username='".$username."'"; 

必须是

$sql  = "SELECT username FROM login WHERE login.username='".$username."'";