检查是否已采用用户名

时间:2015-04-30 07:56:42

标签: php mysql

我已尝试使用我的代码进行各种操作以测试现有用户名,但我无法实现。当alphakey不正确时,它不会插入数据库。我试过写它但它似乎没有成功!

<?php
if (isset($_POST['submit'])) {
    if (!empty($_POST['user']) && !empty($_POST['pass']) && !empty($_POST['alphakey'])) {
        //mysql_real_escape_string() escapes special characters in a string for use in an SQL statement
        $user     = mysql_real_escape_string($_POST['user']);
        $pass     = mysql_real_escape_string($_POST['pass']);
        $alphakey = mysql_real_escape_string($_POST['alphakey']);

        $con = mysql_connect('localhost', '<UserName>', '<Password') or die(mysql_error());
        mysql_select_db('user') or die("cannot select DB");

        $query = mysql_query("SELECT * FROM login WHERE user='" . $user . "'");

        $numrows = mysql_num_rows($query);

        // if user doesn't exist...
        if ($numrows == 0) {
            $encrypt_password = md5($_POST["pass"]);
            $query_key = mysql_query("SELECT * FROM regkey WHERE alphakey='" . $_POST["alphakey"] . "'") or die(mysql_error());
            $check_key = mysql_num_rows($query_key);


            if ($check_key > 0) {
                $sql = mysql_query("INSERT INTO login (user,pass) 
                    VALUES ('" . $_POST["user"] . "','$encrypt_password')") or die(mysql_error());
            }
        } // brace for if($numrows==0)

        if ($sql) {
            echo "<center>Success, You're now registered</center><br/>";
        } else {
            echo "<center>Invalid Alpha Key, Please try again!</center><br/>";
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

你需要检查一下,

  for(var i = 0; i < testObject.length; i++){
        var listItem = "";
        var id = testObject[i].id;
        listItem += liStart + id + liStart2;
        listItem += testObject[i].text;
        listItem += liEnd;
        unorderedList.innerHTML += listItem;
        $("#"+id).data(testObject[i]);
      }