滞后问题导致 AJAX/PHP



所以我正在尝试异步调用PHP脚本,但由于某种原因出现错误:超过最大调用堆栈大小,这使我的网站滞后很多。我的代码中一定有一个循环,但我真的找不到它。我正在寻找过去 3 天,但仍然一无所获。如果有人能发现任何东西,我会非常高兴。提前感谢!

PHP代码:

<?php
    require_once 'li.php'; //File with the constants    
    $Username = $_POST['Username'];
    $query = "SELECT Username
                FROM user_info
                WHERE Username = ?
                LIMIT 1";
    if($stmt = $conn->prepare($query)){   //Prepares the statement!
        $stmt->bind_param('s', $Username); 
        $stmt->execute(); //Executes it!
        if($stmt->fetch()){ //Checks if the query returns anything!
            $stmt->close(); //If yes then closes the prepared statement!
            $error = "Username taken";
            echo $error;
        }   
    }
?> 

AJAX/JS 代码:

$(document).ready(function(){
    $('#Username').on("keyup", function(){
        $.ajax({
            type: 'POST',
            url: 'NameValidation.php', //Your required php page
            data: { Username: Username }, //pass your required data here
            async: true,
        }).done(function(response){
            if(response != ""){
                $('#UsernameLabel').html("Username Taken!");
            }
        });     
    });
});

如果您不明白我想用这段代码做什么,让我再解释一下。我希望每次用户名输入更改时搜索数据库中是否已存在用户名,并通过更改标签文本来提醒用户。

PS:不用担心SQL注入安全性,稍后解决此问题时我会添加它! ^-^

如果

查询未返回任何内容,则不会关闭连接,请尝试将代码更改为:

if($stmt->fetch()){ //Checks if the query returns anything!
     $error = "Username taken";
     echo $error;
}  
$stmt->close(); //close connection in any case

最新更新