服务器端用户注册

时间:2016-08-21 19:39:05

标签: php jquery mysql ajax registration

我正在进行用户注册页面,并且我已经想出如何制作它,以便如果用户从文本输入框中聚焦,则会在其下方显示错误消息。

在JS文件中,我有以下代码

               $("body").delegate("#user_id", "focusout", function(event){
                  event.preventDefault();

                  var c_userid=$('#user_id').val();
                  $.ajax({
                         url: "classes/register.php",
                         method: "POST",
                         data : {getId:1,c_userid:c_userid},
                         success: function(data){
                 $("#userid-error").html(data); 
                  }
                  });
                  });

在应该放入数据的PHP文件中,

   elseif(isset($_POST['getId'])){
    global $conn;
       $c_userid = $_POST["c_userid"];
       $get_id = "SELECT id FROM user_info WHERE id LIKE '%$c_userid%' LIMIT 1";
       $check_id = mysqli_query($conn, $get_id);
       $count_id = mysqli_num_rows($check_id);
            if(empty($c_userid)){
                echo "Please fill user id";
                exit();
            }elseif($count_id > 0){
                echo "ID unavailable";
                exit();
            }

}

现在我可以将SQL Insert编码添加到if语句中,如此...

elseif(isset($_POST['getId'])){
    global $conn;
       $c_userid = $_POST["c_userid"];
       $get_id = "SELECT id FROM user_info WHERE id LIKE '%$c_userid%' LIMIT 1";
       $check_id = mysqli_query($conn, $get_id);
       $count_id = mysqli_num_rows($check_id);
            if(empty($c_userid)){
                echo "Please fill user id";
                exit();
            }elseif($count_id > 0){
                echo "ID unavailable";
                exit();
            }
   }else{
    $first_name = $_POST["first_name"];
    $last_name = $_POST["last_name"];
    $home_phone = $_POST["home_phone"];
    $cell_phone = $_POST["cell_phone"];
    $city = $_POST["city"];
    $state = $_POST["state"];
    $zip_code = $_POST["zip_code"];
    $home_address = $_POST["home_address"];
    $email_address = $_POST["email_address"];
    $user_id = $_POST["id"];
    $password = md5($_POST["password"]);
    $sql = "INSERT INTO user_info (id, password, first_name, "
            . "last_name, city, state, zip_code, home_address, "
            . "email_address, home_address, home_phone, cell_phone)"
            . ""
            . "VALUES($user_id, $password, $first_name, $last_name, $city, $state,
               $zip_code, $home_address, $email_address, $home_address, $home_phone, $cell_phone)";
    $run_query = mysqli_query($conn, $sql);
}

将数据正确插入我的数据库吗?如果用户输入有问题,getID函数是否会阻止通过?

0 个答案:

没有答案