检查用户名是否存在无效

时间:2014-12-13 03:01:28

标签: javascript php mysql if-statement mysqli

我认为该问题与if(data == 1)有关。目前,警报框一直显示可用,当我将其更改为===时,警报框一直显示不可用。我不确定问题出在哪里。

这是我的register.html

                    <form name="registerform" id="registerform" action="register.php" method="post">
                        <div class="form_settings">

                            <p>
                                <label for="firstname">First Name:</label>
                                <input name="firstname" id="firstname" maxlength="32" type="text" placeholder="Optional" />
                            </p>
                            <p>
                                <label for="username" id="usernameText">Username:</label>
                                <input data-parsley-pattern="^[A-Za-z0-9_]{3,15}$" data-parsley-length="[3, 15]" name="username" id="username" maxlength="32" type="text" data-parsley-error-message="Username needs to be between 3 and 15 characters. Case sensitive. No special characters allowed." required/>
                            </p>
                            <p>
                                <label for="password1">Password:</label>
                                <input name="password1" id="password1" data-parsley-pattern="^[A-Za-z0-9_-]{5,25}$" data-parsley-length="[5, 25]" type="password" data-parsley-equalto="#password2" data-parsley-error-message="Passwords must match. Needs to be between 5 and 25 characters. Case sensitive. No special characters allowed." required/>
                            </p>
                            <p>
                                <label for="password2">Confirm Your Password:</label>
                                <input name="password2" id="password2" data-parsley-length="[5, 25]" data-parsley-error-message="Passwords must match. Needs to be between 5 and 25 characters. Case sensitive. No special characters allowed." data-parsley-pattern="^[A-Za-z0-9_-]{5,25}$" type="password" data-parsley-equalto="#password1" required/>
                            </p>
                            <p>
                                <label for="email">E-Mail:</label>
                                <input data-parsley-trigger="change" name="email" id="email" maxlength="1024" type="email" required/>
                            </p>
                            <p style="padding-top: 15px"><span>&nbsp;</span>
                                <input type="submit" class="submit" name="createacc" value="Register" />
                            </p>
                        </div>
                    </form>

这是我的javascript

        $(document).ready(function () {

            $('input[name="createacc"]').click(function (e) {
                var username = $('input[name="username"]').val();
                if (username != '') {
                    e.preventDefault();


                    $.ajax({
                        type: 'POST',
                        data: {
                            username: username
                        },
                        url: 'checkuser.php',
                        success: function (data) { //Receives the data from the php code
                            if(data == 1){  
                                //show that the username is available  
                                alert("available");
                            }else{  
                                //show that the username is NOT available  
                                alert("Unavailable");
                            }  

                        },
                        error: function (xhr, err) {
                            console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                            console.log("responseText: " + xhr.responseText);
                        }
                    });
                    $('#registerform').parsley();
                }
            });


        });

这是我的checkuser.php

<?php
define('DB_SERVER', "localhost");
define('DB_USER', "XXX");
define('DB_PASSWORD', "XXX");
define('DB_DATABASE', "XXX");   
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);    
if(mysqli_connect_errno()) {
    echo "Connection Failed.";
    exit();
}
$username = mysqli_real_escape_string($_POST['username']);     
$result = mysqli_query("SELECT `username` FROM `accounts` WHERE username = '$username'");  
if(mysqli_num_rows($result)>0){  
    echo json_encode(0);   
}else{  
    echo json_encode(1);  
}      
?>

更新 我已经尝试了本帖评论部分发布的所有建议,到目前为止还没有任何工作。不知道该怎么做= /

2 个答案:

答案 0 :(得分:2)

您实际上在返回的字符串中有错误。

2件事:mysqli_real_escape_string() expects exactly 2 parametersmysqli_query() expects 2 parameters

现在这两种方法都需要2个参数,因为您只提供了一个参数,所以它返回错误字符串而不是01。因此,将mysqli连接变量添加为两个方法中的第一个参数。这是替换代码。

$username = mysqli_real_escape_string($mysqli, $_POST['username']);     
$result = mysqli_query($mysqli,"SELECT `username` FROM `ncs_users` WHERE username = '$username'"); 

并尝试使用Parameterized查询,而不是像查询中那样使用$username进行连接,以提高安全性。

答案 1 :(得分:0)

你可以退货

if(mysqli_num_rows($result)>0){  
    echo json_encode(array('vaild' => false));
die;   
}
else{  
    echo json_encode(array('vaild' => true));  
}  

在javascript中设置:

$.ajax({
                        type: 'POST',
dataType: "json",
                        data: {
                            username: username
                        },
                        url: 'checkuser.php',
                        success: function (data) { //Receives the data from the php code
                            if(data.valid == true){  
                                //show that the username is available  
                                alert("available");
                            }else{  
                                //show that the username is NOT available  
                                alert("Unavailable");
                            }  

                        },
                        error: function (xhr, err) {
                            console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                            console.log("responseText: " + xhr.responseText);
                        }
                    });
                    $('#registerform').parsley();
                }
            });

我希望这会对你有所帮助。